-1

I am unclear about use of hashcode and equals method in java.I have following query

First

If I Override only equals method all objects of value Fred are added though HashSet implements set interface,which cant take repeated values.

Second If i override both equals and hashcode only one object is added to HashSet.Why?

Third If I implement only equals in this case will removing one Fred object will remove all?

class Person
{
    String name;
    Person(String name) {
        this.name=name;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Person))
        {
       return false;
         }
    Person p = (Person)obj;
    return p.name.equals(this.name);
       }


    /*@Override
    public int hashCode() {
        return name.hashCode();
    }*/
}

public class HashSetDemo {

    /*
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<Person> s= new HashSet<Person>();
        s.add(new Person("Fred"));
        s.add(new Person("Fred"));
        s.add(new Person("Fred"));
        s.add(new Person("Fred1"));
        for(Person a:s) {
            System.out.println(a.name);
        }

        s.remove(new Person("Fred"));
        System.out.println(s);
    }
}
coder25
  • 2,363
  • 12
  • 57
  • 104

2 Answers2

3

The problem with your equals method is this.name == emp.name this checks if the two strings point to the same memory you should be using equals instead this.name.equals(emp.name). That will solve your first issue.

The hashCode method tells you how to convert to hashCode. Since you made the hashCode equal to the name's hashCode all the Freds have the same hashCode So they all overwrite each other in the HashSet. This answers your second question.

I don't know HashSet's exact implementation but with the First issue solved this won't be a problem.

twain249
  • 5,666
  • 1
  • 21
  • 26
  • I have changed the code by using equals it still prints Fred three times – coder25 Mar 08 '12 at 15:13
  • You didn't uncomment `HashCode`. The `HashCode` method determines whether the `HashSet` will work properly. Look at Gilbert Le Blanc's answer. Also if you do `Person p = new Person("Fred"); Person p1 = new Person("Fred"); System.out.println(p.equals(p1));` you'll get `true` printed. – twain249 Mar 08 '12 at 15:22
1

From "Effective Java" by Joshua Bloch, Item 9:

You must override hashCode in every class that overrides equals. Failure to do so will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.

The Eclipse IDE will generate a hashCode and equals method for you.

Most hashCode methods look something like this:

public int hashCode() {
    int result = 17;
    result = 31 * result + field1;   (int)
    result = 31 * result + field2.hashCode();   (Object)
    ... for the rest of the fields.
    return result;
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111