1

I am trying to understand hashcode(). I have 2 scenarios

1) implementing hashmap with employee2 class objects

2) implementing hashMap with primitives(though they are not primitives)

In case of class objects i understand that if i don't implement hashcode() it generates a random hashcode() every time , so when i retrieve the object , it looks into some different bucket each time and return NULL

But why dosen't this happen in the 2nd case when i don't use class objects

code is as follows:

package programs;
import java.util.*;

public class employee2
{
  private int empid;
  private String name;
  private String dept;
  public employee2(int empid,String name,String dept){
   this.empid=empid;
   this.name=name;
   this.dept=dept;
 }
 int getEmpid(){
    return this.empid;
 }
 @Override public boolean equals(Object o){
      employee2 e=(employee2)o;
     return getEmpid()==e.empid;
  } 

 @Override public int hashCode() {
    int hash = 7;
    hash = 83 * hash + this.empid;
    return hash;
 } 
  @Override public String toString(){
     return empid+", "+name; 
 }


 public static void main(String args[]){
    //HashMap with employee class objects  
       Map<employee2,String> emap=new HashMap<>();    
       emap.put(new employee2(98446,"Amol Singh","Science"),"good");
       emap.put(new employee2(98446,"Robin Singh","Math"),"very good");

   // I get null if i dont override hashcode()    
       System.out.println(emap.get(new employee2(98446,"Robin Singh","Math"))); 

   // New HashMap without Class objects   
       Map<Integer,String> emap2=new HashMap<>();
       emap2.put(23,"amol");
       emap2.put(2,"Robin");

   // I get correct answer without overriding hashcode()    
       System.out.println(emap2.get(23)); 
   }     
}
Amol
  • 303
  • 4
  • 13
  • 1
    `Integer` uses the value of the primitive `int` as hashcode. – Arjan Jun 13 '16 at 04:43
  • 1
    If you take a look at source code of Integer class, it also implementing the hashcode method of its own. – Prabhat Jun 13 '16 at 04:44
  • @zombie so the `hashcode` vale of Integer is the value if int right ? – Amol Jun 13 '16 at 04:53
  • @Amol Check the source code of the Integer class http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/Integer.java#Integer.hashCode%28%29, and looks for hashcode implementation for better understanding. – Prabhat Jun 13 '16 at 05:31
  • @zombie ... yes it returns the hashcode as the value of int ..but since hashcode refers to the index of table array of entry objects , so wouldnt that be a performance issue if int is larger say 1000 – Amol Jun 13 '16 at 07:37
  • @amol As you are saying that hashcode refers to the index of table array of enrty object and array can be accessed using the index in o(1) time. So what exactly you are trying to ask, and is your actual question is not answered yet? – Prabhat Jun 14 '16 at 07:17

4 Answers4

1

Hash-based collections require hashCode to be overriden.

If you don't override it, they won't work fine.

Integer has it's own hashCode implementation, so you don't have to do anything when working with it in collections.

But you have to do this with the classes thay you create, if they are to be placed in hash-based collections

0

If you for example look at the source code of String, there is also a very good algorithm for hashcode already implemented

So for the Wrapper classes and String there will never be need of writing the hashcode method by your own

TheGuy13
  • 21
  • 1
  • 6
0

In your second scenario - when your are putting integer value in a map the primitive type converted to its corresponding object wrapper class. This is called autoboxing. So your int converted to Integer class which already override the hashcode and equals method. Take look at Integer class.

And in your first case you have defined your own class. So you need to supply your own equals and hashcode method.

seal
  • 1,122
  • 5
  • 19
  • 37
0

I found this post, which probably helps to explain it.

what-is-the-default-implementation-of-hashcode

From what I understand you are creating a new Object to key into your map, and the default implementation of hashCode provided by the JVM is treating that object as distinct from your existing object in the map.

Here is the direct link to the java doc for hashcode. Object.hashCode() and the snippet that concerns this question.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects

The moral of the story is that if you are using HashSets with an object you should always define your own hashCode implementation. There are caveats though, for example if you are using singletons.

Community
  • 1
  • 1
waltron
  • 131
  • 9