Tuesday 4 August 2015

How to Compare two Arraylists which contain objects of the same class?

Compare two Arraylists which contain objects of the same class

I have two lists of different sizes which belong to the same class Student which is having attributes like:

  • Integer id;
  • String name;
  • Integer age;


We will compare these two lists and remove the elements which are common
in both of them. Below is the example available to compare two lists:-

Student.java

public class Student {

/**
  * @author Gaurav
  *
  */
private Integer id;
private String name;
private Integer age;


public Student(Integer id, String name, Integer age){
    this.id = id;
    this.name = name;
    this.age = age;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}


public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
    return "Id : " + this.id +" Name : "+this.name+ " age : "+this.age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Student student = (Student) obj;
return id == student.id
&& (name == student.name || (name != null && name
.equals(student.getName())))
&& (age == student.age || (age != null && age
.equals(student.getAge())));
}
}

CompareLists.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

/**
 * @author Gaurav
 *
 */
public class CompareLists {
public static void main( String[] args )
{
  List<Student> l1 = new ArrayList<Student>();
  l1.add(new Student(1,"Kumar",33));
  l1.add(new Student(2,"Hiten",35));
  l1.add(new Student(3,"Varun",46));
  l1.add(new Student(4,"Gaurav",35));
  l1.add(new Student(5,"Vishal",40));


  List<Student> l2 = new ArrayList<Student>();
  l2.add(new Student(4,"Gaurav",35));
  l2.add(new Student(5,"Vishal",40));
  
 
  //This is the first way to compare two list containing objects of same classes and removing the common item.
  
  List<Student> firstList = new ArrayList<Student>(l1);
  System.out.println("First list before removing common elements");
  System.out.println(firstList);
  firstList.removeAll(l2);
  System.out.println("\nFirst list after removing common elements");
  System.out.println(firstList);
  
 //This is the Second way to compare two list containing objects of same classes  and removing the common item.
  List<Student> secondList = new ArrayList<Student>();
  System.out.println("\nSecond list before removing common elements");
  System.out.println(l1);
  for (int i = 0; i < l1.size(); i++){

       if (!l2.contains(l1.get(i))){

           secondList.add(l1.get(i));
       }
   }
  System.out.println("\nSecond list after removing common elements");
  System.out.println(secondList);
  
  //This is the Third way to compare two list containing objects of same classes  and removing the common item.
  List<Student> thirdList = new ArrayList<Student>(l1);
  System.out.println("\nThird list before removing common elements");
  System.out.println(thirdList);
  for(Student apv : l1){
for(Student pv : l2){
if(apv.getId() == pv.getId() && apv.getName().equalsIgnoreCase(pv.getName())){
thirdList.remove(pv);
}
}
}
  System.out.println("\nThird list after removing common elements");
  System.out.println(thirdList);
   
  
  //This is the Fourth way to compare two list containing objects of same classes  and removing the common item.
 
 /**org.apache.commons.collections.CollectionUtils - with the help of this class below call is possible and for this explicitly, we need to add the jar named commons-collections-3.2.1.jar*/
  
  System.out.println("\nFourth list after removing common elements");
  @SuppressWarnings("unchecked")
  Collection<Student> fourthList= CollectionUtils.subtract(l1, l2);
  System.out.println(fourthList);
 
}
}

Result:-

First list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

First list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Second list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Second list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Third list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Third list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Fourth list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

No comments:

Post a Comment