簡體   English   中英

我如何比較有/沒有迭代每個列表的兩個列表

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

我如何比較上面兩個列表中是否有迭代的每個列表?我想比較people1列表而不對人員列表進行迭代。 發生匹配時,它會打印匹配的對象,否則必須打印不匹配的對象。 我不必重復第二個列表就可以這樣做。

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

首先,您需要正確地實現equals方法。 然后,可以使用List#retainAll ,它“僅保留此列表中指定集合中包含的元素(可選操作)。換句話說,從此列表中刪除所有不包含在指定集合中的元素”。

如果要保留原始列表,則將原始列表復制到某個列表,例如: copyofPeople然后可以使用copyofPeople.retainAll(people1) 現在,如果copyofPeople為空( copyofPeople.size()==0 ),則沒有共同的元素。

如果您的邏輯平等取決於對方的電子郵件,那么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;
    }

集合有:

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

如果您使用的是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);
            }
     }

在這里,我重寫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