2
    class Student2{
        int age;
        String name;

        Student2(int age,String name)
        {
            this.age=age;
            this.name=name;
        }

        /*
public int hashCode(){
            return age;     
        }
        */

        public boolean equals(Object o){
            Student2 s2=(Student2)o;
            return (this.age==s2.age && this.name==s2.name);
        }


    }

    public class HashMapDemo{




        public static void main(String a[])
        {   Map m=new HashMap();
        m.put(new Student2(10,"sameer"), 1);
        m.put(new Student2(11,"pagal"), 2);
        m.put(new Student2(12,"ullu"), 3);
        m.put(new Student2(13,"lullu"), 5);

        System.out.println(m.get(new Student2(11,"pagal")));


        }


    }

HashMap... here getting null, i've to overload hashcode() inorder to my respective object. what i've to add in hashCode. my doubt is in hashCode method is it mandatory to override hashCode method.

Martin
  • 594
  • 1
  • 8
  • 32
Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39

5 Answers5

2

you need hashCode and equals methods if you want to use your class as a key. let the be generated by your IDE or use:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
    return result;
}
@Override
public boolean equals( Object obj ) {
    if( this == obj )
        return true;
    if( obj == null )
        return false;
    if( getClass() != obj.getClass() )
        return false;
    Student2 other = (Student2) obj;
    if( age != other.age )
        return false;
    if( name == null ) {
        if( other.name != null )
            return false;
    } else if( !name.equals( other.name ) )
        return false;
    return true;
}
Andreas
  • 303
  • 1
  • 9
1

In your "System.out.println" you are asking for getting an Element you are creating in this moment. This element my has the same properties, like the one you are searching for, but it is a new one. Thus he can not find it. You have to ask for an object that is already inserted. But for this, you have to specifiy wich one you want.

Modify your code, like that:

    Student2 myStud = new Student2(11,"pagal");
    m.put(myStud, 2);

    ...

    System.out.println(m.get(myStud));
Martin
  • 594
  • 1
  • 8
  • 32
1

If you don't override the hashcode() method, then the default Object.hashcode() method will be used which will probably return a different hashcode for each instance even if the object is the same type with the same field values.

If you uncomment your hashcode method you should get your desired result this time. However there are a couple of flaws with your hashcode() and equals() methods.

  1. in your equals method you are checking equality of Strings using == which you shouldn't do. Please use .equals() to compare strings. This only will work since you hardcoded the strings so the compiler will happen to use the same instance for both "pagal"s.

  2. Your hashcode() and equals() methods should both check the same fields. Your hashcode() method only uses age but your equals uses both age and name. Either change your equals to use only age or change hashcode() to use both age and name too.

For more information how to properly implement equals() and hashcode() please read the book Effective Java or look at this IBM java development link

dkatzel
  • 31,188
  • 3
  • 63
  • 67
1

You must override hashcode() and equals() method in Student2 class when used as key in HashMap.
check documentation here

The hashCode method defined by class Object does return distinct integers for distinct objects. This is typically implemented by converting the internal address of the object into an integer and hence you are receiving null.

deejay
  • 575
  • 1
  • 8
  • 24
1

in this case you must override hashcode. if two objects are equal by equal method, then it should have same hashcode. this the way hash map works

        public int hashCode(){
            return age;     
        }

i think this works fine in your case,. but it silently decrease the performance, if you have lot of student under same age. because hash bucket getting bigger sdo on. so try to put some efficient hashcode

subash
  • 3,116
  • 3
  • 18
  • 22
  • what is the purpose of using hashcode method, can not we check the equality of object from equals method?? like List collection. – Piyush Mittal Jul 02 '15 at 03:28