tags:

views:

534

answers:

2

Hiya.

How can I check if two ArrayLists differ from one another? i don't care what's the difference, i just want to know if they're not the same.

thanks!

update

thanks for all of your comments so far, seems that I need to clarify some issues.

I'm fetching scores list from database every minute, and only if the scores list that i fetched is different from the one i fetched a minute ago i want to send it to the client.

now the value of the ArrayList is actually a class that I created (that contains name,lvl,rank,score).

do I need to implement equals() on it ?

+2  A: 

Use equals(). As long as the elements inside the lists implement equals() correctly it will return the correct values.

Unless you want to ignore the order of the values, then you should dump the values in two Set objects and compare those using equals().

Joachim Sauer
If you put the values in a set, you remove duplicates... You will not be checking if they are the same, only if they contain the same elements. This may or may not be what OP wants. But in my opinion, if one list contains 3 x's and one contains only 2, they are not the same.
kgrad
Well, check the length of the two lists first, then dump them into sets if that's what you want.
Lizzan
@Lizzan: That won't work either. The lists ["a", "a", "b"] and ["a", "b", "b"] have the same length, would be equal if converted to sets, but are still not equal for any reasonable definition of equality of lists.
jarnbjo
Right, but if the OP wants to check for that, equals() will work just fine, as Joachim said first. On the other hand, that does make my previous comment rather unuseful as well.
Lizzan
+9  A: 

On the definition of "sameness"

As Joachim noted, for most application, List.equals(Object o) definition works:

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

Depending on how you're using it, though, this may not work as expected. If you have a List<int[]>, for example, it doesn't quite work because arrays inherit equals from Object which defines equality as reference identity.

    List<int[]> list1 = Arrays.asList(new int[] { 1, 2, 3 });
    List<int[]> list2 = Arrays.asList(new int[] { 1, 2, 3 });
    System.out.println(list1.equals(list2)); // prints "false"

Also, two lists with different type parameter can be equals:

    List<Number> list1 = new ArrayList<Number>();
    List<String> list2 = new ArrayList<String>();
    System.out.println(list1.equals(list2)); // prints "true"

You also mentioned that the list must contain elements with the same type. Here's yet another example where the elements don't have the same type, and yet they're equals:

    List<Object> list1 = new ArrayList<Object>();
    List<Object> list2 = new ArrayList<Object>();
    list1.add(new ArrayList<Integer>());
    list2.add(new LinkedList<String>());
    System.out.println(list1.equals(list2)); // prints "true"

So unless you clearly define what equality means to you, the question can have very different answers. For most practical purposes, though, List.equals should suffice.


On implementing equals

Information after update suggests that List.equals will do the job just fine, provided that the elements implement equals properly (because List<E>.equals invokes E.equals on the non-null-elements, per the API documentation above).

So in this case, if we have, say, a List<Player>, then Player must @Override equals(Object o) to return true if o instanceof Player and on the relevant fields, they're all equals (for reference types) or == (for primitives).

Of course, when you @Override equals, you should also @Override int hashCode(). The barely acceptable minimum is to return 42;; slightly better is to return name.hashCode();; best is to use a formula that involves all the fields on which you define equals. A good IDE can automatically generate equals/hashCode methods for you.

See also

  • Effective Java 2nd Edition
    • Item 8: Obey the general contract when overriding equals
    • Item 9: Always override hashcode when you override equals

API links

Related questions

On equals/hashCode combo:

On equals vs ==:

polygenelubricants
thanks for your assitance! I updated the main post with more information.
ufk
you're awesome!! :)
ufk
@polygenelubricants made my life awesome again
c0mrade