How to convert List into Map>, with Java 8 streams and custom List and Map suppliers?

前端 未结 3 517
自闭症患者
自闭症患者 2020-11-30 03:55

It\'s easy to convert List into Map>. For example:

public Map> ge         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 04:29

    You could use this solution, if you plan to create a map similar to Map>:

    Map> ds= requestList.stream().collect(
        Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
        Collectors.mapping(TagRequest::getProperty_2, Collectors.toList()))
    );
    

    If you plan to create a map similar to Map>, you may use:

    Map> ds= requestList.stream().collect(
        Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
        Collectors.mapping(TagRequest::getProperty_2, Collectors.toSet()))
    );
    

提交回复
热议问题