List<VitalityMiddlePotentialSpace> highVitalitySpaceList = potentialSpaceMapper.getGt90PotentialSpace(); //获取高活力空间周边,附近300米点 Set<VitalityMiddlePotentialSpace> highVitalityPeripheryList = new HashSet<>(); highVitalitySpaceList.forEach(highVitalitySpace -> { List<VitalityMiddlePotentialSpace> peripheryList = potentialSpaceMapper.getHighVitalityPeriphery(highVitalitySpace.getGid()); if (ObjectUtils.isEmpty(peripheryList)) { return; } highVitalityPeripheryList.addAll(peripheryList); }); 如何将highVitalityPeripheryList中含有highVitalitySpaceList中的数据删除
时间: 2024-02-03 13:04:05 浏览: 20
可以使用迭代器遍历 highVitalityPeripheryList,判断其中的元素是否在 highVitalitySpaceList 中,如果存在则使用迭代器的 remove() 方法将其删除。具体代码如下:
```
Iterator<VitalityMiddlePotentialSpace> iterator = highVitalityPeripheryList.iterator();
while (iterator.hasNext()) {
VitalityMiddlePotentialSpace peripherySpace = iterator.next();
for (VitalityMiddlePotentialSpace highVitalitySpace : highVitalitySpaceList) {
if (peripherySpace.getGid().equals(highVitalitySpace.getGid())) {
iterator.remove();
break;
}
}
}
```
相关问题
@PostMapping("/queryLicenseReminder") public ApiResult queryLicenseReminder() { Long retailShopId = AccessContext.get().getRetailShopId(); Map<String, List> map = new HashMap<>(3); if (retailShopId != null) { List<GspCompanyLicenseVO> gspCompanyLicenseList = gspCompanyLicenseService.queryLicenseReminder(retailShopId); List<GspUserLicenseVO> gspUserLicenseList = gspUserLicenseService.queryLicenseReminder(retailShopId); List<RetailShopLicenseVO> retailShopLicenseVOList = retailShopLicenseService.queryLicenseReminder(retailShopId); map.put("gspCompanyLicenseList", gspCompanyLicenseList); map.put("gspUserLicenseList", gspUserLicenseList); map.put("retailLicenseList", retailShopLicenseVOList); } return ApiResult.success(map); }改为查询所有门店不按照门店ID查询
可以将原先的 `Long retailShopId = AccessContext.get().getRetailShopId();` 改为获取所有门店的 ID 列表,然后遍历这些门店 ID,逐个查询每个门店的 License Reminder,将每个门店的查询结果存储到一个 List 中,最后将这个 List 作为整个 API 的返回值即可。具体实现可以参考下面的代码:
```
@GetMapping("/queryLicenseReminder")
public ApiResult queryLicenseReminder() {
List<Long> retailShopIdList = retailShopService.getAllRetailShopIds(); // 获取所有门店的 ID 列表
List<Map<String, List>> resultList = new ArrayList<>(); // 存储所有门店的查询结果
for (Long retailShopId : retailShopIdList) {
Map<String, List> map = new HashMap<>(3);
List<GspCompanyLicenseVO> gspCompanyLicenseList = gspCompanyLicenseService.queryLicenseReminder(retailShopId);
List<GspUserLicenseVO> gspUserLicenseList = gspUserLicenseService.queryLicenseReminder(retailShopId);
List<RetailShopLicenseVO> retailShopLicenseVOList = retailShopLicenseService.queryLicenseReminder(retailShopId);
map.put("gspCompanyLicenseList", gspCompanyLicenseList);
map.put("gspUserLicenseList", gspUserLicenseList);
map.put("retailLicenseList", retailShopLicenseVOList);
resultList.add(map);
}
return ApiResult.success(resultList);
}
```
List<String> landsForVillages = feignBasicController.getLandsForVillages(id); List<DevInfo> devByBaseId = feignHdController.getDevByBaseId(userInfo.getId(), classify); List<String> machineIds = devByBaseId.stream().map(DevInfo::getId).collect(Collectors.toList()); Set<String> machineIdList = new HashSet<>(); // json转化为list for (String machineId : machineIds) { List<String> landIdsList = baseMapper.getLandIdsById(machineId); for (String landIdStr : landIdsList) { if(StringUtils.isNotBlank(landIdStr)){ JSONArray arrayStr = JSON.parseArray(landIdStr); for (Object o : arrayStr) { if (landsForVillages.contains(o)) { // 村下面的工作设备 machineIdList.add(machineId); } } } } } 优化一下代码
可以尝试使用 Java 8 的 Stream API 重构代码,如下所示:
```
Set<String> machineIdList = devByBaseId.stream()
.map(DevInfo::getId)
.flatMap(machineId -> baseMapper.getLandIdsById(machineId).stream())
.filter(StringUtils::isNotBlank)
.flatMap(landIdStr -> JSON.parseArray(landIdStr).stream())
.distinct()
.filter(landsForVillages::contains)
.map(machineIdList::add)
.collect(Collectors.toSet());
```
这段代码首先使用 `flatMap` 将 `devByBaseId` 中的每个 `DevInfo` 转换为其对应的 `machineId`,然后对于每个 `machineId`,调用 `baseMapper.getLandIdsById` 获取其对应的 `landIdsList`,并将其转换为一个 `Stream<String>`。接着对于每个 `landIdStr`,使用 `JSON.parseArray` 将其转换为一个 `JSONArray`,然后将其中的每个元素转换为 `String`,并去重。最后,使用 `filter` 过滤出 `landsForVillages` 中包含的元素,并将其对应的 `machineId` 添加到 `machineIdList` 中,最终使用 `collect` 将其转换为一个 `Set<String>`。