

ArrayList or LinkedList to get the contents of that Stream. You can also use any List implementation class e.g. Here you will find methods like toList(), which can be used to convert Java 8 Stream to List. Anyway, It seems they did think about this and provided a class called Collector and Collectors to collect the result of stream operations into different containers or Collection classes.


Similarly, they could have provided convenient methods like toList(), toSet() into Stream class, but unfortunately, they have not done that. when they introduced Path class in JDK 7, which is similar to java.io.File, they provided a toPath() method to File class. Java has a design philosophy of providing conversion methods between new and old API classes e.g. Though class has toArray() method to convert Stream to Array, but there is no similar method to convert Stream to List or Set. When you are processing a List using Stream's map and filter method, you ideally want your result in some collection so that you can pass it to other parts of the program.
#Convert string to list python shapes how to#
One of the common problems while working with Stream API in Java 8 is how to convert a Stream to List in Java because there is no toList() method present in the Stream class.
