list 转map

指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身

 Map<Integer,User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));

 

key 冲突的解决办法,后面的覆盖前面的

 Map<Integer,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2));

posted @ 2022-04-30 22:41  清明雨下  阅读(64)  评论(0)    收藏  举报