Type of Collector returned by Collectors.toList()

Viewed 3079

I am quite puzzled about the Collectors.toList() and Collectors.toSet() static methods. These two methods do not take in any parameters. So how do they know what types of Collector to return?

For example, if we have this line:

Collectors.toList();

The returned Collector is Collector<Object,?,List<Object>>.

If we have this line:

Collector<Integer,?,List<Integer>> c = Collectors.toList();

Then Collectors.toList() will return a Collector<Integer,?,List<Integer>>. Without taking in any input parameters, how does the toList() method know that it needs to return a Collector<Integer,?,List<Integer>>?

Perhaps a sample codes of how toList() is written would be helpful in my understanding.

Thanks in advance.

2 Answers
Related