Converting an array of objects to Stream
:
String[] arr = new String[] {"str1", "str2", "str3"};
Stream<String> stream = Arrays.stream(arr);
Converting an array of primitives to Stream
using Arrays.stream()
will transform the array to a primitive specialization of Stream:
int[] intArr = {1, 2, 3};
IntStream intStream = Arrays.stream(intArr);
You can also limit the Stream
to a range of elements in the array. The start index is inclusive and the end index is exclusive:
int[] values = {1, 2, 3, 4};
IntStream intStream = Arrays.stream(values, 2, 4);
A method similar to Arrays.stream()
appears in the Stream
class: Stream.of()
. The difference is that Stream.of()
uses a varargs parameter, so you can write something like:
Stream<Integer> intStream = Stream.of(1, 2, 3);
Stream<String> stringStream = Stream.of("1", "2", "3");
Stream<Double> doubleStream = Stream.of(new Double[]{1.0, 2.0});