0

I'm very new to java and I have two List<String>. When I do :

list1.equals(list2)

is giving me false. Eventhough the list are same.

Is it the right way to check the equality?

sriram
  • 8,562
  • 19
  • 63
  • 82
  • 3
    we have to know your definition of `equals` – Kent Jan 29 '14 at 14:22
  • 2
    Include code that will let us reproduce your problem. Otherwise we would only have to guess what could be wrong and that means a lot of guessing. – Pshemo Jan 29 '14 at 14:25
  • Please post example input and output. – skiwi Jan 29 '14 at 14:26
  • The `String.equals()` method, the one that `List` uses to check equality, is well-defined, so -**if** they are in the same order- the comparison needs to yield `true`. – skiwi Jan 29 '14 at 14:28

3 Answers3

1

You could use isEqualList from Apache's ListUtils:

isEqualList

public static boolean isEqualList(java.util.Collection list1,
                                  java.util.Collection list2)

Tests two lists for value-equality as per the equality contract in List.equals(java.lang.Object).

This method is useful for implementing List when you cannot extend AbstractList. The method takes Collection instances to enable other collection types to use the List implementation algorithm.

The relevant text (slightly paraphrased as this is a static method) is:

Compares the two list objects for equality. Returns true if and only if 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. Note: The behaviour of this method is undefined if the lists are modified during the equals comparison.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

It is the correct way. Your lists are not equal as you say they are. Try printing them for visual inspection.

Xabster
  • 3,710
  • 15
  • 21
  • 2
    So basically you are saying that this should work and if it is not it means that there is something wrong with lists and OP should print them to see problem? It seems [more like a comment to me rather than answer](http://meta.stackexchange.com/questions/118992/are-works-for-me-answers-valid). – Pshemo Jan 29 '14 at 14:33
1

For two lists to be equal, they need to have the same order. I can only assume that you want to ignore the ordering of the two lists when considering equality.

For a comparison that ignores order, you can compare two Map<String, Integer> instances (where the Integer is the count per string).

eg:

public <T> boolean isEqualIgnoreOrder(List<T> l1, List<T> l2) {
   if (l1.size() != l2.size()) {
      return false;
   }
   Map<T, Integer> m1 = createMap(l1);
   Map<T, Integer> m2 = createMap(l2);
   return m1.equals(m2);
}

protected <T> Map<T, Integer> createMap(List<T> list) {
   Map<T, Integer> map = new HashMap<T, Integer>();
   for (T item : list) {
      Integer prevCount = map.get(item);
      map.put(prevCount == null ? 1 : prevCount + 1);
   }
   return map;
}

This type of collection is known as a bag. More discussion here

Community
  • 1
  • 1
lance-java
  • 25,497
  • 4
  • 59
  • 101