====过滤出集合对象中某个字段满足条件的集合
//获取所有建筑 及下级
List<BuildingAndChildrenVo> buildings = buildingService.getBuildingAndChildren(projectId, buildingPageDTO.getName());
if (CollectionUtil.isNotEmpty(buildings)) {
//过滤出建筑
List<BuildingAndChildrenVo> building = buildings.stream().filter(b -> b.getType() == 3).collect(Collectors.toList());
}
====获取集合对象中某个属性集合
List<DevicePageVM> devicePageVMS = deviceMapper.queryDeviceListByGatewayId(devicePageByGatewayIdDto);
List<String> allBuildingIds = devicePageVMS.stream().map(DevicePageVM::getBuildingRelation).collect(Collectors.toList());
====统计出集合对象中某个数值累计值
List<ConsumptionTrendListVo> hourConsumption = dayMapper.getDayConsumption(projectId, currentStart, currentEnd, deviceIds);
double consumption = hourConsumption.stream().mapToDouble(ConsumptionTrendListVo::getConsumptionSum).sum();
List<DeviceLevelConsVo> deviceCons = deviceConsMap.get(deviceId);
BigDecimal consumption = deviceCons.stream().filter(d -> d.getConsumption() != null).map(DeviceLevelConsVo::getConsumption).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
====根据集合对象中某个字段进行排序
List<DevicePageVM> result = deviceService.queryDeviceListByGatewayId(data);
if (CollectionUtils.isNotEmpty(result)) {
//默认根据设备所属通道进行排序 [正序]
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsLast(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getChannelName, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
if(null != data.getSortName() && !"".equals(data.getSortName())){
switch (data.getSortName()){
case "deviceLocation": //安装设备位置
if("desc".equals(data.getSortEnum())) {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getDeviceLocation, Comparator.nullsFirst(Comparator.naturalOrder())).reversed()).collect(Collectors.toList());
}else {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getDeviceLocation, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
}
break;
case "channelName": //通道名称
if("desc".equals(data.getSortEnum())) {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getChannelName, Comparator.nullsFirst(Comparator.naturalOrder())).reversed()).collect(Collectors.toList());
}else {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getChannelName, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
}
break;
case "slaveAddr": //设备从机地址
if("desc".equals(data.getSortEnum())) {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getSlaveAddr, Comparator.nullsFirst(Comparator.naturalOrder())).reversed()).collect(Collectors.toList());
}else {
result = result.stream().sorted(Comparator.comparing(DevicePageVM::getUpdateTime, Comparator.nullsFirst(Comparator.naturalOrder())).reversed().thenComparing(DevicePageVM::getSlaveAddr, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
}
break;
}
}
}
======对集合对象中某几个属性进行去重
List<DevicePointDataVo> devicePointDataVoList = devicePointDataVoList.stream().collect(
collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUpdateTime() + ";" + o.getValue()))), ArrayList::new)
);
======获取集合对象中某个属性的最大和最小值
List<ConsumptionDto> consumptionDtoList = map.getValue();
ConsumptionDto maxTimeConsumptionDto = consumptionDtoList.stream().max(Comparator.comparing(ConsumptionDto::getTime)).get();
ConsumptionDto minTimeConsumptionDto = consumptionDtoList.stream().min(Comparator.comparing(ConsumptionDto::getTime)).get();
=====根据集合对象的某个属性进行分组
List<Device> deviceList =deviceMapper.getDeviceByChannelIdAndDeviceCategId(data.getChannelId(), data.getDeviceCategId(), data.getDeviceName(), data.getMeterNumber(), "Vasyslink_" + tenantCode);
Map<Long, List<Device>> deviceMap = deviceList.stream().collect(groupingBy(Device::getId));
======对集合对象属性进行比较分组
List<TbStrategyProposal> tbStrategyProposals = deviceMapper.selectTbStrategyProposal(startFormat.format(currentTime), endFormat.format(currentTime));
Map<Long, TbStrategyProposal> collect = tbStrategyProposals.stream().collect(
groupingBy(TbStrategyProposal::getDeviceId,
Collectors.collectingAndThen(
Collectors.reducing((c1, c2) -> c1.getCreateTime().compareTo(c2.getCreateTime()) == 1 ? c1 : c2), Optional::get
)
)
);
=====获取集合对象中某两个属性Map
List<BuildingBaseInfoVo> buildingBaseInfoVos = deviceMapper.queryBuildingNameByIds(buildingIds);
Map<Long, String> buildingNameMap = buildingBaseInfoVos.stream().collect(Collectors.toMap(BuildingBaseInfoVo::getBuildingId, BuildingBaseInfoVo::getName));
=====将集合对象转成Map
List<BaseDeviceType> typeList = deviceMapper.getAllDeviceTypeList(typeIds);
Map<Long, BaseDeviceType> typeMap = typeList.stream().collect(toMap(BaseDeviceType::getId, t -> t));
Java8 Stream处理List对象常用技巧
最新推荐文章于 2024-06-17 06:00:00 发布