简体   繁体   English

我如何比较有/没有迭代每个列表的两个列表

[英]how can I compare the two lists with/without iterating over each list

How can I compare the above two lists with/without iterating over each list?I would like to compare the people1 list without iterating against people list. 我如何比较上面两个列表中是否有迭代的每个列表?我想比较people1列表而不对人员列表进行迭代。 when matching occurs it has print the matched object else it has to print non matching object. 发生匹配时,它会打印匹配的对象,否则必须打印不匹配的对象。 I have to do this without iterating second list. 我不必重复第二个列表就可以这样做。

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);
            }
        }
    }
}

expected output: 预期输出:

matched
Bart : 15 :ramr@gmail.com

First of all, you need have a correct implementation of equals method. 首先,您需要正确地实现equals方法。 Then you can use List#retainAll which "Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection". 然后,可以使用List#retainAll ,它“仅保留此列表中指定集合中包含的元素(可选操作)。换句话说,从此列表中删除所有不包含在指定集合中的元素”。

Copy the original list to some list if you want to keep the original list like: copyofPeople then you can use copyofPeople.retainAll(people1) . 如果要保留原始列表,则将原始列表复制到某个列表,例如: copyofPeople然后可以使用copyofPeople.retainAll(people1) Now if copyofPeople is blank ( copyofPeople.size()==0 ) then there is no element in common. 现在,如果copyofPeople为空( copyofPeople.size()==0 ),则没有共同的元素。

If your logical equality depends on the person's email the then equals method should looks like below: 如果您的逻辑平等取决于对方的电子邮件,那么equals方法应如下所示:

@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;
    }

Collections have: 集合有:

people.retainAll(people1) // for intersection
people.addAll(people1) // for union

if you are using Java 8 then you can also use stream for this. 如果您使用的是Java 8,则还可以使用流。

    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);
            }
     }

Here i am overriding equals method in Person class to return true if all the properties of one object matches other. 在这里,我重写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;
    }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM