[英]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.