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的时候使用这个
浙公网安备 33010602011771号