3

I have a arrayList and i want use contains(Thing o) method to check equality of this two object and I override the equals() method in Thing class but this is not work when I call contains method! this is my thing class:

public class Thing{

private int id;

//getter setter

@Override
public boolean equals(Object o) {
    if(!(o instanceof Thing))
        return false;
    if(id == ((Thing)o).getId())
        return true;
    return false;
}
}

is it necessary to override the hashCode() method too? if yes how to override it?

013
  • 175
  • 1
  • 1
  • 7
  • 5
    Can you show how you used `contains` method? And yes, you should always override both `equals` and `hashcode` when you override one of them. – Rohit Jain Aug 17 '13 at 08:38
  • Overriding hashcode is a good practice but won't help in your case. To override it you can simply `return id;`… – assylias Aug 17 '13 at 08:41
  • `dblist` is List and I have a Thing object with `nlo` name. `if(dblist.contains(nlo)){ //do something }` – 013 Aug 17 '13 at 08:45
  • Post an SSCCE reproducing the problem. – JB Nizet Aug 17 '13 at 08:50
  • nlo is a name of variable? it can contain Thing object with any id. – Alexander Kudrevatykh Aug 17 '13 at 08:51
  • `nlo` has the same `id` with one of the objects in `dblist` – 013 Aug 17 '13 at 09:03
  • Add `if (o == null) { return false; }` to the beginning of your `equals()` method. Also don't forget about `hashCode()` as said. But your code should work even without that things, try iterating over your list and campare ids of every element with your `nlo`, error is definetly somewhere else – SpongeBobFan Aug 17 '13 at 09:19
  • @SpongeBobFan: the null case is already handled by instanceof, which will always return false if the object is null. – JB Nizet Aug 17 '13 at 09:29

3 Answers3

4

You should override hashCode. The ArrayList class doesn't use the hashCode method so it's not necessary now, but if at any point you are going to use your class with HashMap, HashSet, or any other collection that does use hashCode, the program will break because hashCode and equals are not consistent.

A simple implementation of hashCode for this case could be:

public int hashCode() {
    return id;
}
Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
2

Yes, it is essential to override hashCode() whenever u override equals :

  • If you do not overrode HashCode(), the default behaviour from the "Object" class is to give each object a unique hashCode.

  • This unique hashCode would mean two different object instances unless
    you override the hashCode() to give the same value.

    public int hashCode(){ return this.id; }

you dont have to write this manually when you are using Eclipse.:) RightCLick on the java file opened -->Source-->Generate HashCode() and equals()

Aish-Rad
  • 71
  • 4
1

In case you override equals() you should always override hashCode() too. By using Eclipse IDE you simply can generate that methods for you. Menu -> Source -> Generate hashCode() and equals()

My-Name-Is
  • 4,814
  • 10
  • 44
  • 84