java 将list 转为map

2021/12/3 12:06:14

本文主要是介绍java 将list 转为map,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#将list转为map  【注意:要对list 进行判空】

Map<Integer, User> collect = users.stream().collect(Collectors.toMap(User::getUserId, User -> User);
View Code

#将list转为map并且key去重

Map<Integer, User> collect = users.stream().collect(Collectors.toMap(User::getUserId, User -> User,(oldValue, newValue) -> newValue));
View Code #Collectors.toMap() 多字段拼接key,并且去重
 3.1:去掉重复key 【(key1, key2) -> key2)】, key 使用多字段拼接。返回 Map<String, UserTalk> userMap1类型数据
     Map<String, UserTalk> userMap1 = userTalkList.stream().collect(Collectors.toMap(userTalk -> userTalk.getSubjectId() + "" + userTalk.getGradeId() +
         "" + userTalk.getUnitId() + "" + userTalk.getLectureType(), UserTalk -> UserTalk, (key1, key2) -> key2));
View Code

 



这篇关于java 将list 转为map的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程