目录
1、List-->>List:去重
2、List-->>List:提取属性集合
3、List1-->>List2:where id = 1
4、List-->> Map>:group by id,>
5、List-->> Map
1、List-->>List:去重
List根据属性去重
//根据userid去重
userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(paring(User :: getUserid))), ArrayList::new));
2、List-->>List:提取属性集合
提取对象集合中某个属性的集合
Listusers= new ArrayList();
// 取出所有userName集合:
Listnames = user.stream().map(User :: getName).collect(Collectors.toList());
3、List1-->>List2:where id = 1
提取所有ID==1的数据包装为LIST
ListenterAnn = scaleOverview.stream().filter(obj ->
obj.getType().equals("企业年金")).collect(Collectors.toList());
4、List-->> Map>:group by id
分组,Id作为key,数据包装成List放入value
将List数据分组后包装成Map,某属性作为key【一般为id或name】,该key下的数据提取为List存入value
假设有实体类User,里面有属性id,我们将相同id的User进行分组,并存放在Map中。
Map> map = list.stream().collect(Collectors.groupingBy(User::getId));
5、List-->> Map:Map
List包装为某属性作为key,某属性作为value的Map,其中value可能为null,需要做处理
MapaggInvMap = list.stream().collect(Collectors.toMap(AggInvAna::getTypeCode,
p -> {
BigDecimal val = p.getZb() == null ? new BigDecimal("0") : p.getZb();
return val.setScale(2, BigDecimal.ROUND_HALF_UP);
}
));
Map> map = new HashMap<>();
Map> collect = mapper.queryCfgAnaGS(dto).stream().
collect(Collectors.groupingBy(AggInvAna::getManager));
for (Map.Entry> entrie: collect.entrySet()) {
String key = entrie.getKey();
Listlist = entrie.getValue();
MapaggInvMap = list.stream().collect(Collectors.toMap(AggInvAna::getTypeCode,
p -> {
BigDecimal val = p.getZb() == null ? new BigDecimal("0") : p.getZb();
return val.setScale(2, BigDecimal.ROUND_HALF_UP);
}
));
map.put(key, aggInvMap);
}