Java a boxed stream is a stream of the wrapper class instances to simulate a stream of primitives.
1. What is a Boxed Stream?
In Java 8, if we want to convert a stream of objects to a collection then we can use one of the static methods in the Collectors class.
//It works perfectly!! List<String> strings = Stream.of("how", "to", "do", "in", "java") .collect(Collectors.toList());
However, the same process doesn’t work on streams of primitives.
//Compilation Error !! IntStream.of(1,2,3,4,5) .collect(Collectors.toList());
To convert a stream of primitives, we must first box the elements in their wrapper classes and then collect the wrapped objects in a collection. This type of stream is called boxed stream.
Let’s look at a few examples of boxed streams in Java.
2. IntStream (Stream of int
)
Example to convert int stream to List of Integers.
//Get the collection and later convert to stream to process elements List<Integer> ints = IntStream.of(1,2,3,4,5) .boxed() .collect(Collectors.toList()); System.out.println(ints); //Stream operations directly Optional<Integer> max = IntStream.of(1,2,3,4,5) .boxed() .max(Integer::compareTo); System.out.println(max);
Program Output:
[1, 2, 3, 4, 5] 5
3. LongStream (Stream of long
)
Example to convert long stream to List of Longs.
List<Long> longs = LongStream.of(1l,2l,3l,4l,5l) .boxed() .collect(Collectors.toList()); System.out.println(longs); Output: [1, 2, 3, 4, 5]
4. DoubleStream (Stream of double
)
Example to convert double stream to List of Doubles.
List<Double> doubles = DoubleStream.of(1d,2d,3d,4d,5d) .boxed() .collect(Collectors.toList()); System.out.println(doubles); Output: [1.0, 2.0, 3.0, 4.0, 5.0]
Drop me your questions in comments section related to boxed streams or stream of primitives in Java Stream API.
Happy Learning !!
Tim
My question is, is there a way to turn
objArrayOfIntArrays
toList<List<Integer>>
?Praveen
Hi Tim,
Hope it Helps,
Praveen
Praveen