0

I have two arrayLists<myObject>, where myObject is an object of a custom class I've created. I want to be able to compare those arrayLists using the equals() method.

After reading and looking for answers, I've read that certain objects like int[] are only considered equal by the equals() method when they are referencing the same thing.

To fix that, I tried to override the equals method in my custom object. My objects have 3 atributes (all basic types), so my equals method now returns true if all the 3 atributes are equal to those of the object compared, and false otherwise. However, comparing the arraylists still doesn't work. What am I doing wrong?

Excuse me for explaining the code instead of posting it, I do it because the variables and names aren't in English.

EDIT: Ok, here's the code. Compra is my custom class; cantidad,concepto and id are its atributes.

@Override
    public boolean equals(Object obj) {
        boolean result = true;

        if (obj == null) {
            result = false;
        }else{
            Compra comprobada = (Compra) obj;
        if(!(this.id == comprobada.getId())){
        result = false;
       }
        if(!(this.cantidad == comprobada.getCantidad())){
        result = false;
       } if(!this.concepto.equals(comprobada.getConcepto())){
        result = false;
       }

        }
        return result;

    }
kace91
  • 821
  • 1
  • 10
  • 23
  • What do you mean, "still doesn't work"? Post your code, what you expect it to do, and what it actually does. – Jeffrey Bosboom Oct 18 '14 at 23:43
  • If you are worried by the english variables, then write an SSCCE *in english* that demonstrates the problem. Your written explanation is not sufficient for us to help you. – Stephen C Oct 18 '14 at 23:51
  • @StephenCI updated the question to include some code. That is my equals method, I try to compare two arraylists composed by those objects but the result is false when it shouldn't be. – kace91 Oct 18 '14 at 23:56
  • 1
    if `id`, `cantidad`, or `concepto` are Strings, then you need to use `.equals()`, not `==`. – jtahlborn Oct 18 '14 at 23:57
  • @jtahlborn concepto is a String, and it already uses .equals(). The others are an int and a float, that's why I just use ==. – kace91 Oct 19 '14 at 00:02
  • 1
    Check my answer below and tell me if you found something in your code that makes things not work ;) - your code runs fine with my example – Michail Michailidis Oct 19 '14 at 00:27

2 Answers2

1

Based on this one : How can I check if two ArrayList differ, I don't care what's changed

If you have implemented your custom object equals correct (you actually override it and have your one) and the size of the arrayList is the same and each of the pair of the objects is equal then it will return equal. In other words what you are trying to do is totally correct but your arrayLists are not actually having exactly the equal objects in exact order.

Make sure that your equal is called when you check for collection equality by doing a System.out.println() to investigate what is going on. If you don't mind please send the equals of your object.

I run your code in an isolated example and works fine (outtputs true) - I improved the equals method so it doesn't do so many if checks as if only one of them is not equal it should return false.

 class stackoverflow {
    public static void main(String args[]){
        ArrayList<Compra> array1 = new ArrayList<>();
        ArrayList<Compra> array2 = new ArrayList<>();
        array1.add(new Compra(1,2,"test"));
        array2.add(new Compra(1,2,"test"));
        System.out.println(array1.equals(array2));
   }
}
class Compra {
    int id;
    int cantidad;
    String concepto;

    public Compra(int id, int cantidad, String concepto){
        this.id = id;
        this.cantidad = cantidad;
        this.concepto = concepto;
    }



    public boolean equals(Object obj) {


        if (obj == null) {
             return false;
        }else{
             Compra comprobada = (Compra) obj;
             if(!(this.id == comprobada.getId())){
                   return false;
             }
             if(!(this.cantidad == comprobada.getCantidad())){
                   return false;
             } 
             if(!this.concepto.equals(comprobada.getConcepto())){
                   return false;
             }

        }
        return true;
    }



    public int getId() {
        return id;
    }




    public int getCantidad() {
        return cantidad;
    }




    public String getConcepto() {
        return concepto;
    }


}

Some things to check:

  • Are you sure you don't change the order of the things in ArrayList??:
  • Do you print to make sure that these equals checks happen and return true or false as expected?
  • Are you sure that concepto Strings are exactly the same, with the same case and don't contain extra spaces etc?
Community
  • 1
  • 1
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • Hey, thanks for taking the time to write and try the code. I'm still getting the error when comparing. I am pretty sure that the two arrays are equal because they are both created by reading the same file twice. I'm debugging everything just in case I've missed something. – kace91 Oct 19 '14 at 00:47
  • what error? you are probably passing different type of objects? -for sure this code is not where the bug is. Is there an exception? – Michail Michailidis Oct 19 '14 at 00:49
0

As you haven't posted code i suggest you to check into Comparable class and method compareTo and how to use it for custom classes.

charen
  • 371
  • 1
  • 7
  • 20