Link to home
Start Free TrialLog in
Avatar of PearlJamFanatic
PearlJamFanatic

asked on

How can i compare two arraylists in JAVA

The 2 arraylists contains objects of classes as items.
I tried arrallist1.equals(arraylist2) but it gives false een when the contents are same. Can someone tell why?

This is how the lists are created and populated.
ArrayList a=new ArrayList();
a.add(new Person (name, dateOfJoining);




Avatar of ksivananth
ksivananth
Flag of United States of America image

if you use "new Person", though the values of Person is same, it will be treeated as different objects as the default eaqual method just compares its references...

either you have to override eaquals method of Person class to compare by its value or compare each item individually by traversing list!
Avatar of Gurvinder Pal Singh
After overriding the equals and hasdcode method in Person class, you need to loop through a list, pickup an item one by one and check if the other list contains that element.

Your elements needs to be in the same order in both list.

And the class of your objects in the lists needs the have the equals method implemented.

(If not,  the Object.equals() will be used, and that method compares the memorylocation for the objects. )
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are using Eclipse, your can choose 'Generate hashcode and equals..' in your person class.
Pls look at this example..

public static void main(String args[]){
		
		ArrayList<Person> a1 = new ArrayList<Person>();
		ArrayList<Person> a2 = new ArrayList<Person>();
				
		Date d1 = null;
		Date d2 = null;
		
		try {
			d1 = new SimpleDateFormat("MM/dd/yy").parse("05/18/05");
			d2 = new SimpleDateFormat("MM/dd/yy").parse("06/18/85");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
a1.add(new Person("Barbara","Obama",d1));
        a1.add(new Person("Boby","Johns",d1));
        a1.add(new Person("Larry","Stephan",d2));
        
        a2.add(new Person("Boby","Johns",d1));
        a2.add(new Person("Larry","Stephan",d2));

		
		System.out.println(a1.containsAll(a2));
		
		
		for(int i=0;i<a1.size();i++){
			Person p1 = a1.get(i);
			Person p2 = a2.get(i);
			
			String name = p1.getFname();
			String name2 = p2.getFname();
			
			Date d3 = p1.getDob();
			Date d4 = p2.getDob();
			
			if(d3.equals(d4)){
				System.out.println("Both are equal");
			}else{
				System.out.println("Not equal");
			}
			
			if(name.equals(name2)){
				System.out.println("Both equal");
			}else{
				System.out.println("Not Equal");
			}
		}
}

Open in new window

u can easily implement ur logic in above example..by just using some flags.If both name&string are equal then we can say that both are equal...
Avatar of PearlJamFanatic
PearlJamFanatic

ASKER

@gurvinder372 as suggested by you in  33821310 If i check that the
a) element  count is equal in both and
b) each element in 2nd arraylist exists in 1st (a.contains(b.get(i))
will this be sufficient to declare the 2 equals?
Yes, if you have overridden hashcode and equals method, then  a.contains(b.get(i)) should be enough.

Ok I generated them in eclipse. Quick question does contains use hashcode or equals by any chance? just want to know why we are overriding them
There is an arraylist within this class and when i try to generate the hashcode() and equals() it gives me warning saying the arraylist does not implement either and hence it may not work correctly. should this be ignored?
If you want to know why we are overriding them, please check links i posted in #33821321.

contains method will probably loop through the list and check each item (using overridden equals method), to check if they are same.