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. 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. 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".
Copy the original list to some list if you want to keep the original list like: copyofPeople
then you can use copyofPeople.retainAll(people1)
. Now if copyofPeople
is blank ( copyofPeople.size()==0
) then there is no element in common.
If your logical equality depends on the person's email the then equals method should looks like below:
@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.
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.
@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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.