简体   繁体   English

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

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

I have implemented the following example: 我已经实现了以下示例:

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

How can I get an output of Map<String, List<EventDto>> map instead? 如何获取Map<String, List<EventDto>> map的输出?

An EventDto can be obtained by executing an external method which converts an Event to an EventDto . 一个EventDto可以通过执行其中转换外部方法获得EventEventDto For example - this::convertFromEventToEventDto . 例如 - this::convertFromEventToEventDto

You need a mapping Collector to map the Event elements to EventDto elements : 您需要一个mapping Collector来将Event元素mappingEventDto元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM