根据相等的属性和时间/日期的比较从列表中删除对象

时间:2019-05-24 10:49:30

标签: java

假设我有一个对象Person具有以下属性:

public class Person {
   private int customerNumber
   private java.sql.Date birthday
   private java.sql.Time birthTime
}

CustomerNumber不是唯一字段,许多人可以共享同一号码。

现在,如果我将这些Person对象的集合存储在列表List<Person> persons中,并且我想根据其生日/时间删除具有相同客户编号的人员,则如果有两个(或更多) )具有相同customerNumber的人,则应将第一个出生的人保留在列表中,而将其他人删除。

例如,如果人员列表将包含以下对象:

[{customerNumber: 123, birthday: '1970-10-21', birthTime: '18:20:10'},
 {customerNumber: 123, birthday: '1975-10-21', birthTime: '18:20:10'},
 {customerNumber: 123, birthday: '1970-10-21', birthTime: '10:00:00'},
 {customerNumber: 456, birthday: '1990-02-15', birthTime: '14:50:20'}]

做完一些魔术之后,我应该得到以下列表:

[{customerNumber: 123, birthday: '1970-10-21', birthTime: '10:00:00'},
 {customerNumber: 456, birthday: '1990-02-15', birthTime: '14:50:20'}]

因为有3个人具有相同的客户编号(123),但第三个高居第一个出生(基于日期和时间),因此他/他应保留在列表中,而其他两个应删除。 / p>

3 个答案:

答案 0 :(得分:2)

假设您具有相应的getter和setter,则可以尝试以下操作:

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 :(得分:0)

它可能很慢,因为正在使用两个循环,但是您可以使用与此类似的循环

public List<Person> removeDuplicate(List<person> personList){

    Iterator<Person> itr = personList.iterator();
    boolean isRemoved = false;

    while (itr.hasNext()) {

        Person item = (Person) itr.next();

        for(Person element: personList){
              //i am assuming you have a getter method for all your properties in Person class
             if(element.getCustomerNumber() == item.getcustomerNumber()){

                 if(element.getBirthday().compare(item.getBirthday() < 0){

                     itr.remove();
                     isRemoved = true;
                     break; 
                  }  
              }
         }

        if(isRemoved){

           Continue;      
         }
      }
}   

答案 2 :(得分:0)

可以通过使用hashMap和类似于Person类中实现的compare方法来解决您的问题。 我尝试了以下程序,它对我有用。

 import java.sql.Date;
    import java.sql.Time;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class P3 {

        public static void main(String[] args) {
            // Create sample data.
            List<Person> persons = new ArrayList<>();
            persons.add(new Person(1, new Date(2019, 1, 5), new Time(2, 0, 0)));
            persons.add(new Person(1, new Date(2019, 1, 5), new Time(4, 0, 0)));
            persons.add(new Person(1, new Date(2019, 1, 5), new Time(5, 0, 0)));
            persons.add(new Person(1, new Date(2019, 1, 5), new Time(1, 0, 0)));
            persons.add(new Person(2, new Date(2019, 1, 5), new Time(2, 0, 0)));

            // Create map with unique customer number.
            Map<Integer, Person> map = new HashMap<>();
            for(Person person : persons) {
                if(map.containsKey(person.getCustomerNumber())) {
                    int eleToBeRemoved = Person.compare(map.get(person.getCustomerNumber()), person);
                    if(eleToBeRemoved == 1) {
                        map.remove(person.getCustomerNumber());
                        map.put(person.getCustomerNumber(), person);
                    }
                } else {
                    map.put(person.getCustomerNumber(), person);
                }
            }

            persons = new ArrayList<>(map.values());
            for(Person person : persons) {
                System.out.println(person);
            }


        }

    }

    class Person {

        private int customerNumber;
        private java.sql.Date birthday;
        private java.sql.Time birthTime;

        public Person() {
        }

        public Person(int customerNumber, Date birthday, Time birthTime) {
            this.customerNumber = customerNumber;
            this.birthday = birthday;
            this.birthTime = birthTime;
        }

        public int getCustomerNumber() {
            return customerNumber;
        }

        public void setCustomerNumber(int customerNumber) {
            this.customerNumber = customerNumber;
        }

        public java.sql.Date getBirthday() {
            return birthday;
        }

        public void setBirthday(java.sql.Date birthday) {
            this.birthday = birthday;
        }

        public java.sql.Time getBirthTime() {
            return birthTime;
        }

        public void setBirthTime(java.sql.Time birthTime) {
            this.birthTime = birthTime;
        }


        public static int compare(Person p1, Person p2) {
            if(p1.getCustomerNumber() == p2.getCustomerNumber()) {
                int birthdayComp = p1.getBirthday().compareTo(p2.getBirthday());
                if(birthdayComp == 0) {
                    int timeComp = p1.getBirthTime().compareTo(p2.getBirthTime());
                    if(timeComp == 0) {
                        return 2;
                    } else if(timeComp < 0) {
                        return 2;
                    } else {
                        return 1;
                    }
                } else if(birthdayComp < 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
            return 0;
        }

        @Override
        public String toString() {
            return "Person [customerNumber=" + customerNumber + ", birthday=" + birthday + ", birthTime=" + birthTime + "]";
        }

    }