Java 8 lambda Collectors.groupingBy with map in Collector.toList ()

I applied the following example:

Map<String, List<Event>> map = events.getItems().stream() .collect(Collectors.groupingBy(Event::getStatus, Collectors.toList())); 

How can I get the result Map<String, List<EventDto>> map instead?

An EventDto can be obtained by executing an external method that converts Event to EventDto . For example, this::convertFromEventToEventDto .

+5
source share
1 answer

You will need a mapping Collector to map Event elements to EventDto elements:

 Map<String, List<EventDto>> map = events.getItems() .stream() .collect(Collectors.groupingBy(Event::getStatus, Collectors.mapping(this::convertFromEventToEventDto, Collectors.toList()))); 
+9
source

Source: https://habr.com/ru/post/1265696/


All Articles