如何使用对象';Java中的s ID和LocalDateTime字段?

如何使用对象';Java中的s ID和LocalDateTime字段?,java,list,arraylist,stream,Java,List,Arraylist,Stream,假设我们有以下Dto模型 public class OrderDto { Long id; LocalDateTime drawDate; } 我们有下面的列表,我需要对其进行筛选,并且还包括我需要的结果 List<OrderDto> orderDtoList = new ArrayList<>(); orderDtoList.add(new OrderDto(1L, LocalDateTime.now())); // 1 o

假设我们有以下Dto模型

public class OrderDto {
    Long id;
    LocalDateTime drawDate;
}
我们有下面的
列表
,我需要对其进行筛选,并且还包括我需要的结果

List<OrderDto> orderDtoList = new ArrayList<>();

orderDtoList.add(new OrderDto(1L, LocalDateTime.now()));              // 1
orderDtoList.add(new OrderDto(1L, LocalDateTime.now().plusDays(1)));  // 2
orderDtoList.add(new OrderDto(1L, LocalDateTime.now().plusDays(2)));  // 3
orderDtoList.add(new OrderDto(2L, LocalDateTime.now().minusDays(1))); // 4
orderDtoList.add(new OrderDto(2L, LocalDateTime.now()));              // 5
orderDtoList.add(new OrderDto(2L, LocalDateTime.now().plusDays(1)));  // 6
orderDtoList.add(new OrderDto(2L, LocalDateTime.now().plusDays(2)));  // 7
orderDtoList.add(new OrderDto(3L, LocalDateTime.now().minusDays(2))); // 8
orderDtoList.add(new OrderDto(3L, LocalDateTime.now().minusDays(1))); // 9
orderDtoList.add(new OrderDto(3L, LocalDateTime.now()));              // 10

List<OrderDto> desiredList = new ArrayList<>();
desiredList.add(new OrderDto(1L, LocalDateTime.now().plusDays(1)));   // 2
desiredList.add(new OrderDto(2L, LocalDateTime.now().plusDays(1)));   // 6
desiredList.add(new OrderDto(3L, LocalDateTime.now()));               // 10
List orderDtoList=new ArrayList();
orderDtoList.add(neworderdto(1L,LocalDateTime.now());//1.
orderDtoList.add(新的OrderDto(1L,LocalDateTime.now().plusDays(1));//2.
orderDtoList.add(新OrderDto(1L,LocalDateTime.now().plusDays(2));//3.
orderDtoList.add(新的OrderDto(2L,LocalDateTime.now().minusDays(1));//4.
添加(新的OrderDto(2L,LocalDateTime.now());//5.
orderDtoList.add(新的OrderDto(2L,LocalDateTime.now().plusDays(1));//6.
orderDtoList.add(新的OrderDto(2L,LocalDateTime.now().plusDays(2));//7.
orderDtoList.add(新的OrderDto(3L,LocalDateTime.now().minusDays(2));//8.
orderDtoList.add(新的OrderDto(3L,LocalDateTime.now().minusDays(1));//9
添加(新的OrderDto(3L,LocalDateTime.now());//10
List desiredList=新的ArrayList();
desiredList.add(新订单数据到(1L,LocalDateTime.now().plusDays(1));//2.
desiredList.add(新的OrderDto(2L,LocalDateTime.now().plusDays(1));//6.
desiredList.add(neworderdto(3L,LocalDateTime.now());//10
正如您所看到的,我们有几个
OrderDto
对象具有相同的ID,但我需要在所需列表中包括
drawDate
多于
LocalDateTime.now()
的对象但是只有大于
LocalDateTime.now()的第一个对象
。显然,我需要的是带有
plusDays(1)
的对象,而不是
plusDays(2)
。所需列表中不能有具有相同ID的对象

同样重要的是。如果具有相同ID的对象的
drawDate
值不大于
LocalDatetime.now()
。然后我需要选择具有最新
drawDate
的对象
所需列表中所示

还有一件事。所需列表中不能有具有相同ID的对象。甚至可能有10个
OrderDto
对象具有相同的id,但选择的一个将是第一个
drawDate
大于
LocalDateTime.now()的对象


我正试图想出一个Java流,可以为我做到这一点,但遇到了麻烦欢迎任何其他解决方案。

我想出了一种方法,可以帮我完成这项工作。如果有人能想出更好的办法,请随意发布。我之所以使用排序,是因为列表可能不会按提取日期排序,所以我只是想确保在从中提取任何元素之前对它们进行排序。它确保我得到了正确的一个。然后我反转列表,以获取第一个日期最接近
LocalDateTime.now()
的对象

private List getFilteredList(List orderDtoList){
List filteredList=新建ArrayList();
LocalDateTime now=LocalDateTime.now();
List sortedList=orderDtoList.stream().sorted(Comparator.comparing(OrderDto::getDrawDate)).collect(Collectors.toList());
for(订单到dto:sortedList){
if(dto.getDrawDate().isAfter(现在)和&filteredList.stream().noneMatch(orderDto->Objects.equals(orderDto.getId(),dto.getId())){
filteredList.add(dto);
}
}
收款。反向(分类列表);
for(订单到dto:sortedList){
if(dto.getDrawDate().isBefore(now)和&filteredList.stream().noneMatch(orderDto->Objects.equals(orderDto.getId(),dto.getId())){
filteredList.add(dto);
}
}
返回过滤器列表;
}

您尝试过什么?你会犯什么错误?请阅读。不要从流开始。试着这样做,看看你找到的解决方案是否可以使用基于流的方法进行重构(并保持可读性)。@P.J.Meisch,我没有什么可炫耀的。完成我的任务也没什么要紧的。我在尝试不同的流组合。没什么大不了的,我同意他的建议。对于循环来说,这似乎相当简单,但使用流会让人非常痛苦。@VGR是的,这看起来很简单,但对于循环meh来说仍然无法做到。欢迎任何帮助
 private List<OrderDto> getFilteredList(List<OrderDto> orderDtoList) {

    List<OrderDto> filteredList = new ArrayList<>();
    LocalDateTime now = LocalDateTime.now();

    List<OrderDto> sortedList = orderDtoList.stream().sorted(Comparator.comparing(OrderDto::getDrawDate)).collect(Collectors.toList());

    for (OrderDto dto : sortedList) {
        if (dto.getDrawDate().isAfter(now) && filteredList.stream().noneMatch(orderDto -> Objects.equals(orderDto.getId(), dto.getId()))) {
            filteredList.add(dto);
        }
    }

    Collections.reverse(sortedList);
    for (OrderDto dto : sortedList) {
        if (dto.getDrawDate().isBefore(now) && filteredList.stream().noneMatch(orderDto -> Objects.equals(orderDto.getId(), dto.getId()))) {
            filteredList.add(dto);
        }
    }

    return filteredList;
}