aspose file tools
The moose likes Beginning Java and the fly likes How do I can compare two arraylists? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How do I can compare two arraylists?" Watch "How do I can compare two arraylists?" New topic
Author

How do I can compare two arraylists?

D Hagy
Greenhorn

Joined: Jul 05, 2011
Posts: 12
I have two arraylists like arraylist1 and arraylist2. I need to compare them and if they are not equal I need to take another action, than if they are equal. How I can compare all ints in these arrays and how I can compare them? I think one equal method is not enough.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Are you talking about ArrayLists or about arrays?
Usually, simply using the equals method of Lists should be good enough. This will use the equals method of all its elements. However, sometimes that's not good enough, in those cases where the elements have an equals method you can't use. Arrays are such objects.

I am assuming you have a List<int[]>. If this is correct, then you indeed cannot use equals itself. You will need to create a utility method that checks if two List<int[]> objects are equal. They are equal if:
1) they have the same size
2) each element is equal

You need to iterate over the elements of both Lists; you can then use Arrays.equals to check for the array equality.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9944
    
    6

The first thing you need to do is decide for yourself what you consider equal array lists to be. A few things to consider:

1) Can arrays of different sizes still be equal? If one array is size 10, and another is 5, can they still be equal?

2) Does the order of the elements in the array matter?


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How do I can compare two arraylists?
 
Similar Threads
create a key from a string.
Comparing two ArrayList
compare object
Compare 2 HashMap<String, ArrayList>
object comparison