The Collectors.toList Method

The Collectors.toList Method

//The following example retrieves the names of each member in the 
//collection roster and groups them by gender:

	
Map<Person.Sex, List<String>> namesByGender =
    roster
        .stream()
        .collect(
            Collectors.groupingBy(
                Person::getGender,                      
                Collectors.mapping(
                    Person::getName,
                    Collectors.toList())));

Accumulates the stream elements into a new instance of List.

As with most operations in the Collectors class, the toList operator returns an instance of Collector, not a collection.

Related concepts

The Collectors.toList Method

The Collectors.toList Method — Structure map

Clickable & Draggable!

The Collectors.toList Method — Related pages: