随笔 - 5,  文章 - 0,  评论 - 0,  阅读 - 2082

1、指定字段属性进行剔重

list中的对象,根据指定一个字段,进行剔重

//获取查询到的单号数据
List<EmpDischargeDTO> empDischargeDTOList = empDischargeDAO.findByOrgCode(dischargeDTO);

//联合查询可能存在重复值,需要根据billCode进行剔重
List<EmpDischargeDTO> collect = empDischargeDTOList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(EmpDischargeDTO::getBillCode))), ArrayList::new));

2、List转String

对于String类型的List,将所有对象合并为一个String对象,并在每个之间加入逗号隔开

String s1 = studentList.stream().map(Student::getName).collect(Collectors.joining(","));

上面代码是对studentList中的所有student的name属性进行合并加逗号操作,输出结果为:name1,name2,name3......

3、List中数字类型求和

①对象多个属性,对单独一个double或int类型进行求和:

对double类型进行求和

double sum = resultList.stream().mapToDouble(EmpDischargeDTO::getTotalQty).sum();

对int类型进行求和

double sum = resultList.stream().mapToInt(EmpDischargeDTO::getQty).sum();

上面两个列子,后面的sum()是求和,还可以最大,最小,平均等操作

②List集合为Double或Integer类型进行求和

Double类型的List求和

double sum = doubleList.stream().collect(Collectors.summarizingDouble(value -> value)).getSum();

Integer类型的List求和

long sum = integerList.stream().collect(Collectors.summarizingInt(value -> value)).getSum();

上面两个列子,后面的getSum()是求和,还可以最大,最小,平均等操作

list转map

HashMap<String, String> map03 = list.stream().collect(HashMap::new, (map, item) -> map.put(item.getUserName(), item.getSex()), HashMap::putAll);

value可能为null的时候使用这个

posted on   仿生海绵  阅读(124)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示