Using streams, how can I map the values in a HashMap?

后端 未结 3 625
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 08:00

Given a Map where Person has a String getName() (etc) method on it, how can I turn the Map

3条回答
  •  情歌与酒
    2020-12-17 08:15

    One way is to use a toMap collector:

    import static java.util.stream.Collectors.toMap;
    
    Map byNameMap = people.entrySet().stream()
                                         .collect(toMap(Entry::getKey, 
                                                        e -> e.getValue().getName()));
    

提交回复
热议问题