#将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