java 将list 转为map

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

java 将list 转为map
Map<Integer, User> collect = users.stream().collect(Collectors.toMap(User::getUserId, User -> User);
View Code

#将list转为map并且key去重

java 将list 转为map
Map<Integer, User> collect = users.stream().collect(Collectors.toMap(User::getUserId, User -> User,(oldValue, newValue) -> newValue));
View Code #Collectors.toMap() 多字段拼接key,并且去重 java 将list 转为map
 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

 

上一篇:js——获取数组中的最大值


下一篇:Pandas 数据处理