1、利用lambda表达式过滤集合
过滤集合获取想要的属性集合
defectList,获取所有WoMold这个属性为1的集合。
defects = defectList.stream().filter(s -> s.getWoMold() != null && s.getWoMold().equals(1)).collect(Collectors.toList());
过滤集合获取交集
获取equipmentList集合中ptid和ptIds的交集。也就是equipmentList中ptid在ptIds这个范围里面的
//查找线路所属设备列表
List<PoleTower> poleTowerList1 = poleTowerList.stream().filter(s -> s.getBranchId().equals(branchId)).collect(Collectors.toList());
List<Long> ptIds = CollectionUtils.extractToList(poleTowerList1, "id");
List<Equipment> equipmentList1 = equipmentList.stream()
.filter((Equipment s) -> ptIds.contains(s.getPtId()))
.collect(Collectors.toList());
如
ptIds = ['1','2','3']
equipmentList = {[ptid:'1',name:'hh'],[ptid:'2',name:'h'h],[ptid:'3',name:'hh'],[ptid:'4',name:'hh']}
返回结果应为
equipmentList = {[ptid:'1',name:'hh'],[ptid:'2',name:'hh'],[ptid:'3',name:'hh']}
2、利用lambda表达式去重
altermList根据TerminalNo去重
//告警涉及的设备数(根据设备no去重得到)
altermList = altermList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>
(Comparator.comparing(Alterm::getTerminalNo))), ArrayList::new));