我有一个基本的SpringBoot应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件 我有这段代码来比较POJO,但是隔离似乎不起作用,因为lastDeviceEvent和firstDeviceEvent是具有相同ID的同一对象
DeviceEvent lastDeviceEvent = null;
DeviceEvent firstDeviceEvent = null;
try {
lastDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getId().equals(deviceId))
.sorted(comparing((DeviceEvent de) -> de.getId()).reversed())
.findFirst().get();
firstDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getId().equals(deviceId))
.sorted(comparing((DeviceEvent de) -> de.getId()))
.findFirst().get();
LOG.info("lastDeviceEvent --> " + lastDeviceEvent.getId());
LOG.info("firstDeviceEvent -> " + firstDeviceEvent.getId());
} catch (NoSuchElementException nse) {
throw new AccessDeniedException("403 Forbidden");
}
答案 0 :(得分:0)
比较器似乎是正确的。问题似乎出现在您的过滤条款中,您将事件ID 与设备ID 进行比较
lastDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getDeviceId().equals(deviceId)) // Original code used getId()
.sorted(comparing((DeviceEvent de) -> de.getId()).reversed())
.findFirst()
.get();