-1

I have a question for the following code in the best answer on this page:

return people
     .filter( p -> p.age() < 19)
     .collect(toList());

Where is the implementation of toList()? java.util.stream.Collectors.toList()? I don't understand why toList() can be here. How does it works?

blackeyedcenter
  • 188
  • 1
  • 12

2 Answers2

3

toList() means Collectors.toList() with java.util.stream.Collectors.toList is being statically imported.

import static java.util.stream.Collectors.toList;

Stream#collect expects a Collector

<R, A> R collect(Collector<? super T, A, R> collector);

and Collectors.toList() returns a Collector

public static <T> Collector<T, ?, List<T>> toList() { ... }
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

It's a static method in the Collectors interface - see here: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toList--

Mimo
  • 59
  • 1
  • 4