//It works perfect !! List<String> strings = Stream.of("how", "to", "do", "in", "java") .collect(Collectors.toList());
The same process doesn’t work on streams of primitives, however.
//Compilation Error !! IntStream.of(1,2,3,4,5) .collect(Collectors.toList());
To convert a stream of primitives, you must first box the elements in their wrapper class and then collect them. This type of stream in called boxed stream.
IntStream to List of Integers
Example to convert int stream to List of Integers.
List<Integer> ints = IntStream.of(1,2,3,4,5) .boxed() .collect(Collectors.toList()); System.out.println(ints); Output: [1, 2, 3, 4, 5]
LongStream to List of Longs
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]
DoubleStream to List of Doubles
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.
Happy Learning !!
All of my websites are hosted on Bluehost. Why? Because they make it incredibly easy to get a new site up and running with their one-click automatic WordPress Blog installation.
You can use your Bluehost account to host multiple domains—no need to pay more for hosting when you’re ready to start another website.