The toArray
method defined in the Stream class returns an array of the elements of the stream. The toArray
is a terminal operation.
package org.wesome.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
class Apple {
public static void main(String args[]) {
List<String> fruits = new ArrayList<>();
fruits.add("Macintosh");
fruits.add("Fuji");
fruits.add("Gala");
fruits.add("Jonagold");
System.out.println("*---------------------------to Array on list of Strings---------------------------*");
/* paas Array into a generator function */
String[] apples = fruits.stream().toArray(String[]::new);
System.out.println("apples = " + Arrays.toString(apples));
/* paas size of the Array */
apples = fruits.stream().toArray(size -> new String[size]);
System.out.println("apples = " + Arrays.toString(apples));
System.out.println("*---------------------------String split into array---------------------------*");
String appleName = "Macintosh, Fuji, Gala, Jonagold";
String[] appleArray = Arrays.stream(appleName.split(",")).map(String::trim).toArray(String[]::new);
System.out.println("appleArray = " + Arrays.toString(appleArray));
System.out.println("*---------------------------to Array on primitive ints---------------------------*");
int[] intStream = IntStream.rangeClosed(1, 5).toArray();
System.out.println("intStream = " + Arrays.toString(intStream));
System.out.println("*---------------------------convert Array back to Stream---------------------------*");
int[] arr = new int[]{1, 2, 3, 4, 5};
/* Stream.of() also calls Arrays.stream() internally */
IntStream stream = Arrays.stream(arr);
stream.forEach(System.out::println);
System.out.println("*---------------------------to Array on Wrapper Integers---------------------------*");
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Integer[] integers = list.stream().toArray(Integer[]::new);
System.out.println("integers = " + Arrays.toString(integers));
System.out.println("*---------------------------Integers List to int array---------------------------*");
int[] ints = list.stream().mapToInt(Integer::intValue).toArray();
System.out.println("ints = " + Arrays.toString(ints));
System.out.println("*---------------------------List of String Integers to int array---------------------------*");
List<String> stringList = Arrays.asList("10", "20", "30", "40", "50");
ints = stringList.stream().mapToInt(Integer::parseInt).toArray();
System.out.println("ints = " + Arrays.toString(ints));
System.out.println("*---------------------------List of Integers to String array---------------------------*");
String[] stringInt = IntStream.rangeClosed(0, 10).mapToObj(Integer::toString).toArray(String[]::new);
System.out.println("stringInt = " + Arrays.toString(stringInt));
}
}
toArray
function on User-defined object
package org.wesome.java8;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Data
@AllArgsConstructor
class Fruit {
int fruitId;
String fruitName;
String taste;
double price;
}
class Apple {
public static void main(String args[]) {
List<Fruit> fruits = Arrays.asList(new Fruit(1, "Macintosh", "sweet", 1.1), new Fruit(2, "Fuji", "sweet", 2.2), new Fruit(3, "Gala", "sour", 1.1), new Fruit(4, "Jonagold", "sour", 2.2));
Fruit[] apples = fruits.stream().toArray(Fruit[]::new);
System.out.println("apples = " + Arrays.toString(apples));
System.out.println("*---------------------------convert Array back to List---------------------------*");
List<Fruit> fruitList = Arrays.stream(apples).collect(Collectors.toList());
System.out.println("fruitList = " + fruitList);
}
}