i want to know how the hashtable retreives the data
With Regards,<br />Ajay
public class Hash {public static void main(String[] args) {Hash h1 = new Hash();Hash h2 = new Hash();HashMap hm = new HashMap();hm.put(h1,"first");hm.put(h2,"second");System.out.println(hm.get(h1));System.out.println(hm.get(h2));}public boolean equals(Object o) {return false;}public int hashCode() {return 1;}} output :nullnull
At the time of searching any object , what happens is , first JVM retrieves the hashcode of object then find bucket & then with the help of equals method it find the actual object .
With Regards,<br />Ajay
if we cross check to the code that u hve written
when the compiler encounters the line "System.out.println(hm.get(h1));".... it first computes the hashcode of h1 and according to the above code it should return 1. So once it gets the hashcode it goes to that particular bucket in the hashtable,but according to the above code it gets 2 key-value pairs. One is "h1, first" and the second one is "h2, second".So from here how does the equals method help us in getting the desired object.
Think that the above particular code works properly...
With Regards,<br />Ajay
With Regards,<br />Ajay
With Regards,<br />Ajay
Can 2 unequal objects hve there hashcode same...
Is it possible for 2 or more objects to have the same hashcode???
Say if two or more objects have the same hashcode, then is it necessary for their equal methods to return true???
Can u just brief me abt the organization of the hashtable??
1] if two object are equal then their hash code must be same .
2] If two object having different hash code then they must not be equal .
4> Say i overide the hashcode method is it must for me to override the equals method???
With Regards,<br />Ajay
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |