1

I want to compare two ArrayList of string arrays.

   List<String[]> list1 = new ArrayList<String[]>;
   List<String[]> list2 = new ArrayList<String[]>;

   list1.equals(list2);

This will return false because equals method in ArrayList will do equals on the element.

    ListIterator<E> e1 = listIterator();
    ListIterator<?> e2 = ((List<?>) o).listIterator();
    while (e1.hasNext() && e2.hasNext()) {
        E o1 = e1.next();
        Object o2 = e2.next();
        if (!(o1==null ? o2==null : o1.equals(o2)))
            return false;
    }
    return !(e1.hasNext() || e2.hasNext());     

If you do equals on array, it will check reference equality. Is there anyway we can use list1.equals(list2) instead of checking each element in array list.

4
  • you need to keep the information that your list contains arrays (so that should be at least ListIterator<E[]>, then use Arrays.equals instead of o1.equals(o2)) Commented Jul 27, 2015 at 3:31
  • What about if you flatten your List to one dimension then compare? Or can you change from List<String[]> to List<List<String>>? Commented Jul 27, 2015 at 4:15
  • @njzk2 that is the equals method in ArrayList. Unless i create a wrapper class, I can't change this Commented Jul 27, 2015 at 5:17
  • @Manikandan. No. I am mentioning Arrays.equals that's in the Arrays class. Commented Jul 27, 2015 at 13:23

3 Answers 3

1

You can not do it by using equals. Instead of doing equals, you can do retainAll(). You can just write a small function, say isEqual() and use this where you are using equals. You need to convert your array as list and pass it to this function.

{ 
  ListIterator<E> e1 = listIterator();
  ListIterator<?> e2 = ((List<?>) o).listIterator();
  while (e1.hasNext() && e2.hasNext()) {
      E o1 = e1.next();
      Object o2 = e2.next();
      if (!(o1==null ? o2==null : isEqual(Arrays.alList(o1),Arrays.asList(o2))))
        return false;
   }
   return !(e1.hasNext() || e2.hasNext()); 
}

     boolean isEqual(List list1, List list2){

          int originalSize = list1.size();
          list1.retainAll(list2); 
          // list1 will retain all the elements that are present in list2
          // if list1 has all the elements that are present in list2, present list1 size will be equal to `original size`
          if(list1.size() == originlaSize){
              returns true;
          }else{
              return false;
          }

     }
Sign up to request clarification or add additional context in comments.

5 Comments

When you downvote, you should better explain the reason.
the op clearly states they don't want reference equality of the arrays.
It seems unnecessary to create new lists from the arrays just to compare their values.
@SotiriosDelimanolis But the OP does not want to do it manually, If he is worried about performance, he can just sort one of the array do it in O(nlogn)
@karthik that would work. Note that there is Arrays.equals, that pretty much does what your isEqual does, only on arrays
1

You can use list1.equals(list2) if you change from List<String[]> to List<List<String>>.

public static void main(String[] args) throws Exception {
    List<List<String>> list1 = new ArrayList() {{
       add(new ArrayList(Arrays.asList("a", "b", "c"))); 
       add(new ArrayList(Arrays.asList("d", "e", "f")));
       add(new ArrayList(Arrays.asList("g", "h", "i")));
    }};
    List<List<String>> list2 = new ArrayList()  {{
       add(new ArrayList(Arrays.asList("a", "b", "c"))); 
       add(new ArrayList(Arrays.asList("d", "e", "f")));
       add(new ArrayList(Arrays.asList("g", "h", "i")));
    }};

    System.out.println(list1);
    System.out.println(list2);
    System.out.println(list1.equals(list2));
}

Results:

[[a, b, c], [d, e, f], [g, h, i]]
[[a, b, c], [d, e, f], [g, h, i]]
true

Otherwise, you're probably looking something along the lines of:

public static void main(String[] args) throws Exception {
    List<String[]> list1 = new ArrayList() {{
       add(new String[] {"a", "b", "c"}); 
       add(new String[] {"d", "e", "f"});
       add(new String[] {"g", "h", "i"});
    }};
    List<String[]> list2 = new ArrayList()  {{
       add(new String[] {"a", "b", "c"}); 
       add(new String[] {"d", "e", "f"});
       add(new String[] {"g", "h", "i"});
    }};

    System.out.println(listsEqual(list1, list2));
}

public static boolean listsEqual(List<String[]> list1, List<String[]> list2) {
    if (list1.size() != list2.size()) {
        return false;
    }

    for (int i = 0; i < list1.size(); i++) {
        if (!Arrays.equals(list1.get(i), list2.get(i))){
            return false;
        }
    }
    return true;
}

Results:

true

2 Comments

Thanks.. I think changing List<List<String> will be the simpler. Another option is creating wrapper class on ArrayList and override equals method. I dont want to do it.
@Manikandan You're welcome. If you found my answer useful to solve your question, kindly click the check mark next to my answer.
-1

Make a simple wrapper for the array class.

private String[] array;
public ArrayWrapper(String[] array) {
    this.array = array;
}

Override equals(Object obj) to use Arrays.equals(array, (String[]) obj) and just hashCode() is just array.hashCode().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.