java - Little Confusion Over Hashing -


consider ther following code :

public class carequalstestagain {  string model;      public carequalstestagain(string x) {         this.model=x;     }      @override     public int hashcode(){               //bad hashcode         system.out.println("__hash__");         return 1;     }      @override     public boolean equals(object o){         system.out.println("in equals");         if((o instanceof carequalstestagain) && ((carequalstestagain)o).model==this.model){             return true;         }         else             return false;     }      public static void main(string[] args) {          map map=new hashmap();          carequalstestagain car1=new carequalstestagain("8");         map.put(car1, "red");         system.out.println("key1 : "+map.get(car1));         //line 1          carequalstestagain car2=new carequalstestagain("8");         system.out.println("key2 : "+map.get(car2));         //line 2          carequalstestagain car3=car1;         system.out.println("key3 : "+map.get(car3));         //line 3          carequalstestagain car4=new carequalstestagain("9");         map.put(car4, "red");         system.out.println("key4 : "+map.get(car4));         //line 4          carequalstestagain car5=new carequalstestagain("10");         map.put(car5, "red");         system.out.println("key5 : "+map.get(car5));          //line 5          carequalstestagain car6=new carequalstestagain("8");         map.put(car6, "green");         system.out.println("key6 : "+map.get(car6));          //line 6          key=(string)map.get(car1);         system.out.println("key1 : "+key);                    //line 7      }  } 

prints output as:

__hash__ __hash__ key1 : red  __hash__ in equals key2 : red  __hash__ key3 : red  __hash__ in equals __hash__ key4 : red  __hash__ in equals in equals __hash__ key5 : red  __hash__ in equals in equals in equals __hash__ in equals in equals in equals key6 : red  __hash__ in equals in equals key1 : green 

my question :

1) when each object created jvm calculate hashcode , put in bucket or when hashmap put() method called jvm uses key object calculate hashcode ?

2) put() , get() calls both hashcode , equals method. hence put() method calls overrided equals() correctly depending upon no. of objects in bucket seen in output line 1,4,5,6. get() method not same. line 1 get() doesn't call equal(), line 2 did, line 3,4,5 didn't call, line 6 did. line 7 didn't why?

3) equals(object o) method compares passed object i.e object o objects resides in bucket given hashcode. why did doesn't stop comapring when found 1 early. ex - in bucket 1928 car1 car4 car5 resides, when car6 calls get() calls equals() if car6 compares car1 , found equal should stop comparing, instead compares 3 times. why?

when each object created jvm calculate hashcode , put in bucket or when hashmap put() method called jvm uses key object calculate hashcode ?

it calls hashcode() when needs - in case when call put() or get().

for line 1 get() doesn't call equal(), line 2 did, line 3,4,5 didn't call, line 6 did. line 7 didn't why?

you're getting confused own diagnostics. call:

system.out.println("key1 : "+map.get(car1));  

is equivalent to:

object tmp = map.get(car1); system.out.println("key1 : " + tmp); 

so call equals occurs before print key1. make simpler understand, should change diagnostics to:

system.out.println("test 1"); carequalstestagain car1 = new carequalstestagain("8"); system.out.println("created car"); map.put(car1, "red");     system.out.println("added car map"); object tmp = map.get(car1); system.out.println("result of get: " + tmp); 

that way it's clearer happens when. in general, both put() , get() need to:

  • call hashcode() on key you're adding/fetching
  • if hash code matches existing ones in map, call equals() each match, until either runs out of matches or finds equal object

3) equals(object o) method compares passed object i.e object o objects resides in bucket given hashcode. why did doesn't stop comapring when found 1 early. ex - in bucket 1928 car1 car4 car5 resides, when car6 calls get() calls equals() if car6 compares car1 , found equal should stop comparing, instead compares 3 times. why?

you're assuming compares car1 first. hash maps unordered - there's no guarantee order candidates equal hash codes compared in. will stop when finds match though. if change hash code more sensible, it's need check 1 item.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -