Java Language Arrays Arrays to Stream


Java SE 8

Converting an array of objects to Stream:

String[] arr = new String[] {"str1", "str2", "str3"};
Stream<String> stream =;

Converting an array of primitives to Stream using will transform the array to a primitive specialization of Stream:

int[] intArr = {1, 2, 3};
IntStream intStream =;

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 =, 2, 4);

A method similar to 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});