List转Map


//以userid为主,重复数据不获取,不会抛出异常
Map<Long, UserLoginLog> longDateMap = userLoginLogList.stream()
.collect(Collectors.toMap(UserLoginLog::getUserId, Function.identity(), (key1, key2) -> key1));

//业务逻辑
if(longDateMap.containsKey(listVo.getUserId()))
  listVo.setLastActivityTime((Objects.nonNull(longDateMap.get(listVo.getUserId()))) ? null : longDateMap.get(listVo.getUserId()).getCreateTime());



//以下会抛出异常
Map<Long, String> userMap = userInfoList.stream()
.collect(Collectors.toMap(k -> k.getUserId(), k -> k.getNickName()));
Map<Long, CustomerInfo> customerInfoMap = customerInfoList.stream().
collect(Collectors.toMap(k -> k.getUserId(), k -> k));

//业务逻辑
if(userMap.containsKey(listVo.getUserId()))
  listVo.setNickName(userMap.get(listVo.getUserId()));
if(customerInfoMap.containsKey(listVo.getUserId())){
listVo.setKycTier(customerInfoMap.get(listVo.getUserId()).getIdentityCheckLevelNumber());

 

 
posted @ 2024-02-27 12:01  霸气小青年  阅读(8)  评论(0编辑  收藏  举报