In this tutorial you’ll see how to convert a Stream to list using collector method and utility methods like toList() and toCollection() of Collectors class in Java Stream API . 1. A simple example to collect Stream elements into an ArrayList . import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamToList { public static void main(String[] args) { Stream<String> streamElem = Stream.of("A", "B", "C", "D"); List<String> listFromStream = streamElem.collect(Collectors.toList()); System.out.println(listFromStream.getClass().getName()); System.out.println("Elements in the list- " + listFromStream); } } Output java.util.ArrayList Elements in the list- [A, B, C, D] As you can see type of the List returned is ArrayList. 2. If you want to convert the Stream into a LinkedList then you can use Collectors.toCollection() method. public class StreamToLi
Java, Spring, Web development tutorials with examples