ListHealthDeviceResult deviceResults = healthDeviceResultServicegetAllResultByBarcodebarcode;如何去除里面deviceType相同itemName相同但checkTime不同的重复数据
可以使用Java 8的流操作来去除重复数据。可以按照deviceType和itemName进行分组,并选择其中checkTime最新的数据。
List<HealthDeviceResult> uniqueResults = deviceResults.stream()
.collect(Collectors.groupingBy(result -> result.getDeviceType() + result.getItemName()))
.values().stream()
.map(list -> list.stream()
.max(Comparator.comparing(HealthDeviceResult::getCheckTime))
.orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList());
首先,通过Collectors.groupingBy方法将结果按照deviceType和itemName进行分组,得到一个以deviceType和itemName为键,以对应结果列表为值的Map。
然后,通过stream().max()方法找到每个分组中checkTime最新的数据,如果列表为空则返回null。
接下来,使用filter(Objects::nonNull)过滤掉null值,最后使用Collectors.toList()将结果收集为一个新的List
原文地址: https://www.cveoy.top/t/topic/iyIZ 著作权归作者所有。请勿转载和采集!