Friday, 15 July 2016

Retrieve a List from Stream in Java 8

Retrieve List from java.util.Stream in Java 8

In the previous article I explained the new class introduced in Java 8 called Optional.
Today I was working with streams and needed to convert it to a List. So thought of sharing this..

Go here if you need to Convert a List to Stream.
Stream API in Java 8 does not define a direct operation or standard to convert a stream data,
into a List. Java 8 design pattern unfortunately does not allow direct conversions from the Stream
to Collections like List or Set.

However not everything is lost, as the developers have given us the
concept of Collectors.You can read more about collectors in my Simple Collector Tutorial.
Using Collectors we can easily achieve inter conversions from Stream to Collections.

There are various method explaining how to convert Stream to List in Java 8.
Here are the most common ones.

The Collectors.toList() method

The Collectors.toList() method is the most straightforward method to convert Stream to List in java 8.
It involves some simple code.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toList());

System.out.println(newList);  //[Convert, Stream, to, List]

Most of the daily requirements will be addressed by the above method
but if in case you are the odd one out who just can't do thing as the normal
people do, then you might look at the below options as well.

The Collectors.toCollection() method to convert a Stream to List in Java8

This is quite same as the previous one except for the fact that it is more generalized
conversion. The toCollection() can be used to convert Stream to other collection class also
like for example a TreeSet.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=streamData.collect(Collectors.toCollection(ArrayList::new ));
//TreeSet<String> set=streamData.collect(Collectors.toCollection(TreeSet::new ));

System.out.println(newList);  //[Convert, Stream, to, List]

Using forEach() to convert a Stream to List in Java 8

Ok so this is a crude way of converting a Stream to a List. But if the other options
are not good enough for you then you might try out this one. Here what we will traverse
through the elements of the Stream using the forEach() loop and add each element to the List.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
System.out.println(newList);  // [Convert, Stream, to, List]

Using forEachOrdered() to convert a Stream to ordered List in Java 8

The forEach() will work with Streams pretty good. But there is a catch.
In case of parallel streams where multiple Stream data is processed horizontally
it will break the ordering. So you will end up with a jumbled list of objects from the initial
Stream. The forEachOrdered() helps to traverse a parallel stream in an orderly way. So the
list you will get will maintain the initial order of elements as present in the parent Stream.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=new ArrayList<String>();
System.out.println(newList);  // [Convert, Stream, to, List]

Using toArray() and Arrays.asList() to convert a Stream to List in Java 8

If you are really looking to lower efficiency, readability and clarity of our code
and you do not like the normal stuff that others do then you can try this out.
Jokes apart, this method can be used to learn the features of Stream and also to
understand the basics of Streams and Collections in Java 8.

Stream<String> streamData=Stream.of("Convert","Stream","to","List");
List<String> newList=(List<String>)(Object)Arrays.asList(streamData.toArray());

System.out.println(newList);  // [Convert, Stream, to, List]


To conclude we can adopt various steps to convert Stream to List in Java 8.
Since there is no direct way to do this I leave it upto you to choose the best method
for your development activity.However I prefer the first two methods using Collectors.

SEE ALSO: Convert a List to Stream in Java 8.

No comments:

Post a Comment

Coder Magnet
CoderMagnet is full of resources from our daily development activities. It has solutions for common problematic scenarios in technologies like Java 8, AEM, JCR and also occasionally gives you tips on Blogger as well.