Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare List<String[]>

I have two List<String[]>'s (string array lists), and I'd like to match the contents with each other. Obviously == doesn't do the trick, but .equals () doesn't seem to do it either. So how should I check whether the contents of the string array lists are the same?

By the way, in both above cases I got an exception with message null.

EDIT: Ok... for some reason only x.equals(y) works, and not y.equals(x). Odd...

like image 239
RobinJ Avatar asked Sep 07 '12 18:09

RobinJ


2 Answers

Perhaps the easiest solution would be to use two List<List<String>>s instead. Assuming the List implementations used extend AbstractList, using equals will give you the desired behavior. From the documentation for AbstractList.equals:

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.

You can easily wrap a String[] in a thin List<String> implementation that extends AbstractList by using Arrays.asList.

EDIT: Here's an example:

String[] array1 = {"1", "2", "3"};
String[] array2 = {"4", "7"};

String[] array3 = {"1", "2", "3"};
String[] array4 = {"4", "7"};

List<List<String>> lst1 = new ArrayList<>();
lst1.add(Arrays.asList(array1));
lst1.add(Arrays.asList(array2));

List<List<String>> lst2 = new ArrayList<>();
lst2.add(Arrays.asList(array3));
lst2.add(Arrays.asList(array4));

System.out.println(lst1.equals(lst2)); //prints true
like image 190
Paul Bellora Avatar answered Nov 01 '22 02:11

Paul Bellora


Typically you should avoid dealing with Arrays. they are ugly and lead to these kind of problems. If possible use List<List<String>> then you can use .equals() normally.

if you insist, you could use a custom isequal implementation like below. the key is to use Arrays.equals()

public class DemoEquals {
    List<String[]> listOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listOneOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"});
    List<String[]> listTwo = (List<String[]>) Arrays.asList(new String[]{"2one1", "2one2"}, new String[]{"2two1"});

    private boolean isEqual(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;
    }
    @SuppressWarnings("unchecked")
    private void isEqual() {
        //prints true
        System.out.println(isEqual(Collections.EMPTY_LIST, Collections.EMPTY_LIST));
        //prints true
        System.out.println(isEqual(listOne, listOne));
        //prints true
        System.out.println(isEqual(listOne, listOneOne));
        //prints false
        System.out.println(isEqual(listOne, listTwo));
        //prints false
        System.out.println(isEqual(listOne, Collections.EMPTY_LIST));

    }
    public static void main(String[] args) {
        new DemoEquals().isEqual();
    }
}
like image 33
Andreas Petersson Avatar answered Nov 01 '22 02:11

Andreas Petersson