This example will show how to convert a Stream into a List using Java 8. A Collector is a reduction operation that will accumulate elements from a stream into a cumulative result. We must supply a Collector in order to gather all the Stream’s items in a List which we can do by using a connivence method toList() from the Collectors utility. Collectors.toLists() is probably the most frequently used collectors used.
@Test
public void stream_to_list() {
List<String> abc = Stream.of("a", "b", "c")
.collect(Collectors.toList());
assertTrue(abc.size() == 3);
}
Or you could staticly import the Collectors class to reduce the Stream into a List.
...
import static java.util.stream.Collectors.toList;
...
@Test
public void stream_to_list() {
List<String> abc = Stream.of("a", "b", "c")
.collect(toList());
assertTrue(abc.size() == 3);
}