[Solved] Remove objects from list based on equal property and comparison of time/date [closed]


Assuming you have the corresponding getters and setters, you may try something like below:

List<Person> unique = persons.stream()
                     .collect(Collectors.groupingBy(Person::getCustomerNumber)) //returns a Map<String,List<Person>> with customerNumber as key
                     .values()
                     .stream()   // stream and sort each list 
                     .map(e-> e.stream().sorted(
                        Comparator.comparing(Person::getBirthday)
                                  .thenComparing(Person::getBirthTime))
                       .findFirst().get())    // map to first Person obj
                     .collect(Collectors.toList());  

unique.forEach(System.out::println);

1

solved Remove objects from list based on equal property and comparison of time/date [closed]