代码之家  ›  专栏  ›  技术社区  ›  jcrshankar

如何比较这两个列表,同时/不遍历每个列表

  •  -1
  • jcrshankar  · 技术社区  · 8 年前

    如何比较以上两个列表,并对每个列表进行迭代?我想比较people1列表,而不是迭代people列表。当匹配发生时,它会打印匹配的对象,否则它必须打印不匹配的对象。我必须在不重复第二个列表的情况下完成这项工作。

    import java.util.*;
    
    public class Person implements Comparable<Person> {
        String name;
        int age;
        String mail;
    
        public String getMail() {
            return mail;
        }
    
        public void setMail(String mail) {
            this.mail = mail;
        }
    
        public Person(String name, int age, String mail) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    
        public String toString() {
            return name + " : " + age;
        }
    
        public int compareTo(Person p) {
            return getMail().compareTo(p.getMail());
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
    
            if (obj == null) {
                return false;
            }
    
            if (getClass() != obj.getClass()) {
                return false;
            }
    
            return false;
        }
    
        public static void main(String[] args) {
            List<Person> people = new ArrayList<Person>();
    
            people.add(new Person("Homer", 38, "shankar@gmail.com"));
            people.add(new Person("Marge", 35, "shankar6@gmail.com"));
            people.add(new Person("Bart", 15, "ramr@gmail.com"));
            people.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
    
            System.out.println("\t" + people);
    
            List<Person> people1 = new ArrayList<Person>();
    
            people1.add(new Person("jug", 38, "jug@gmail.com"));
            people1.add(new Person("benny", 35, "benny@gmail.com"));
            people1.add(new Person("Bart", 15, "ramr@gmail.com"));
            people1.add(new Person("Lisa", 13, "ramkumar@gmail.com"));
    
            for (Person people : people) {
                if (people1.contains( people)) { 
                    System.out.println("matched");
                    System.out.println(people);
                } else {
                    System.out.println("not matched");
                    System.out.println(people);
                }
            }
        }
    }
    

    预期产量:

    matched
    Bart : 15 :ramr@gmail.com
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   Amit Bera    8 年前

    首先,您需要正确实现 equals 方法。那你就可以用 List#retainAll 它“只保留此列表中包含在指定集合中的元素(可选操作)。换言之,从该列表中删除指定集合中未包含的所有元素”。

    如果要保持原始列表如下所示,请将原始列表复制到某个列表: copyofPeople 那你就可以用 copyofPeople.retainAll(people1) . 现在如果 复制人 是空白的( copyofPeople.size()==0 )那就没有共同点了。

    如果逻辑上的相等取决于此人的电子邮件,则等于方法应如下所示:

    @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (mail == null) {
                if (other.mail != null)
                    return false;
            } else if (!mail.equals(other.mail))
                return false;
            return true;
        }
    
        2
  •  1
  •   JustCurious    8 年前

    Collections 拥有:

    people.retainAll(people1) // for intersection
    people.addAll(people1) // for union
    
        3
  •  1
  •   Deep    8 年前

    如果您使用的是Java 8,那么您也可以使用stream来实现这一点。

        for (Person p : people) {
                if(people1.stream().filter(i -> i.equals(p)).findFirst().isPresent()){
                    System.out.println("matched");
                    System.out.println(p);
                }
                else{
                    System.out.println("not matched");
                    System.out.println(p);
                }
         }
    

    在这里,我重写Person类中的equals方法,如果一个对象的所有属性都匹配另一个对象,则返回true。

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person p = (Person) obj;
            if(getName() == p.getName() && getAge() == p.getAge() && getMail() == p.getMail())
                return true;
            else
                return false;
        }