Search code examples
javaarraysequalsdeepequals

equals / Arrays.deepequals returns false on identical arrays


I'm trying to test if two arrays are identical, I printed out every values inside them and you can see that those values are the same but for some reason the equality test always returns false. I've tried using equals as well as Arrays.deepEquals but none of those two methods work.

Here's my code:

System.out.println("========");
for (int i = 0; i < row.length; i++) {
    System.out.println("----------");
    System.out.println(row[i].getAlias());
    System.out.println(row[i].getDisplay());
    System.out.println(row[i].getModify());
    System.out.println(row[i].getName());
    System.out.println(row[i].getType());
    System.out.println(row[i].getOp());
    System.out.println(row[i].getValue());
    System.out.println("----------");
}
System.out.println("========");

for (ResultContainer rc : editedResultsList) {
    System.out.println("========");
    for (int i = 0; i < rc.getResult().length; i++) {
        System.out.println("----------");
        System.out.println(rc.getResult()[i].getAlias());
        System.out.println(rc.getResult()[i].getDisplay());
        System.out.println(rc.getResult()[i].getModify());
        System.out.println(rc.getResult()[i].getName());
        System.out.println(rc.getResult()[i].getType());
        System.out.println(rc.getResult()[i].getOp());
        System.out.println(rc.getResult()[i].getValue());
        System.out.println("----------");
    }
    System.out.println("========");
    System.out.println(rc.getResult().equals(row));
    System.out.println(Arrays.deepEquals(rc.getResult(), row));
    System.out.println(rc.getResult() == row);

    if (rc.getResult().equals(row)) {
         editedResultsList.remove(rc);
    }
}

and here's the output:

========
----------
null
false
false
mep_ident
NUMBER
insert
0
----------
----------
Numéro Partenaire
true
true
mep_ptrpartident
NUMBER
insert
000
----------
----------
Numéro Produit
true
true
mep_ptrpasident
NUMBER
insert
111
----------
----------
Code Document
true
true
mep_code_doc
VARCHAR2
insert
222
----------
----------
Production ?
true
true
mep_production
VARCHAR2
insert
333
----------
========
========
----------
null
false
false
mep_ident
NUMBER
insert
0
----------
----------
Numéro Partenaire
true
true
mep_ptrpartident
NUMBER
insert
000
----------
----------
Numéro Produit
true
true
mep_ptrpasident
NUMBER
insert
111
----------
----------
Code Document
true
true
mep_code_doc
VARCHAR2
insert
222
----------
----------
Production ?
true
true
mep_production
VARCHAR2
insert
333
----------
========
false
false
false
true

Solution

  • From the Javadoc:

    Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:
    - e1 and e2 are both arrays of object reference types, and Arrays.deepEquals(e1, e2) would return true
    - e1 and e2 are arrays of the same primitive type, and the appropriate overloading of Arrays.equals(e1, e2) would return true.
    - e1 == e2
    - e1.equals(e2) would return true.

    About the class of the objects you have in your arrays, make sure that overrides equals() and hashcode() properly.

    UPDATE

    Consult the Object javadoc, basically you need to add this to your class:

    @Override
    public boolean equals(Object other) {
        // Add here code to verifiy that this object is "equals"
        // to the method argument "other".
        // return true if they are equal or false otherwise
    }
    
    @Override
    public int hashCode() {
        // Return here a "hash" integer (two objects equals to each other
        // must return the same hashcode)
    }