lambda实际使用总结
1: List<map> 中通过几个已知的值,获取未知的值
Map map = allDefectList.stream().filter(o-> o.get("THEDAY").equals(day) && o.get("DEFECT_TYPE_NEWNAME").equals("defectName")).findFirst().orElse(null);
2: java中使用lambda表达式检查list集合是否存在某个值
boolean bool = list.stream().anyMatch(a -> a.getName().equals("a") || a.getNick().equals("a"));
3: list 对象求某个字段的和
Integer result = list.stream().collect(Collectors.summingInt(Student::getAge)); System.out.println("所有学生年龄之和 : " + reuslt);
int planSum = jobList.stream().mapToInt(PcPlanJob::getPlanTarget).sum();
4: list 对象中拼接字符串
List<AqxStorehouseInfos> storehouseInfoList = storehouseInfosMapper.selectBatchIds(param.getStorehouseInfoIds()); String storehouseStr = storehouseInfoList.stream().map(AqxStorehouseInfos::getStorehouseName).collect(Collectors.joining ("|"));
5: list<Integer> 转化为 String 并以逗号,分隔开
private List<Integer> storehouseTypeIds; String storehouseTypeIds= param.getStorehouseTypeIds().stream().sorted().map(String::valueOf).collect(Collectors.joining (","));
6: list<Entity> 中某个字段分组, 并判断值数量
Map<Object, Long> mapGroup = infoList.stream().collect(Collectors.groupingBy(info -> info.getProductId(), Collectors.counting())); for (Map.Entry<Object, Long> entry : mapGroup.entrySet()) { if (Convert.toInt(entry.getValue())>1) throw new BizException("500", "一个子任务只能分配一次"); }