![]() Using the bucketing as above it can safely ignore some of the entries/objects to compare for equality. Also while existing entry is retrieved (get() called) it has to compare with all the objects in the HashMap until it finds a match. If no bucketing is used, then everytime a new entry is put it has to compare with all existing (previously put) objects to ensure uniqueness. Why does it behave like thatįor better performance. Infact, HashSet uses HashMap behind the scene. The same applies to HashSet a Set implementation that also uses the same hashing. Hence it is not safe to change the key objects content after used to store in map. When asked to find for "123456", there is no key matching "123456", though was originally put with that value it is now "654321" in the map. Because the object k3.name change affects key stored in the map bucket to change from "123456" to "654321". Guess what happens when get(k4) is called ! The bucket will be located and = fails due to the given key is a separate object, then equals is also called on all 3 objects in map bucket but equals fails and final result of get(k4) is false. ![]() ![]() Now if we take a separate Dog k4 = new Dog("123456") and also change k3.name to "654321". It is because the change in key content preserved the hashCode value of 6. The result of get() method here will be the expected non-null object. Here the = equality check succeeds for k3 and equals() method is not called (this bit is picked up from David Wallace answer, any up votes on this point please give him not me!). Remember here the same object k3 which was used to put is changed, so the change reflects in HashMap also, it is the same one object. Scenario 2:īut if k3 name is changed to "654321" which is of length 6, then the bucket will be found and the equals() will be called to compare with 3 object that were put in the bucket, and also the equals() returns true for one object that is same k3. This is the case of corrupted entry which can never be retrieved, unless the key content is changed again to bring its hashCode value back to 6. When I come searching for "you" using home your address, if I cannot find your home in first place, There is no question of compare names of household members. Due to only one bucket that holds objects with hashCodes of 6 and no bucket of hashCode of 4, it results in no bucket found and no attempt will be made to use equals() method. Assuming only k1, k2 and k3 are put into the HashMap, when get(k3) is called (now having name "1234") will result in looking for a bucket that holds objects with hashCode 4. Scenario 1:įor example if the Dog k3 name is changed to "1234", then the hashCode would now become 4. ![]() So, if the key object content is changed after a key (Dog object) is used to store (put) the key-value pair into the HashMap, it may not be possible find/retreive the original object (sometimes it may). When get() method is called, the HashMap locates the bucket with same hashCode of the given key, if found then on each of the object in bucket it first uses simple = equality check to see if given key and object in bucket are same objects, if not then equals() method equality check to find for exact object. All 3 Dog objects names here are of length 6, hence go into one bucket. ![]() So,for example Dog k1 = new Dog("clover"), Dog k2 = new Dog("clever") and Dog k3 = new Dog("123456") are all stored in one bucket due to the hashCode() method of Dog returns length of name as hashCode value. All the key objects that have same hashCode value are stored in one bucket. In HashMap the objects are stored in groups called buckets (of keys). (Caveat: This is not a bad example as it impacts a lot in term of performance.) /**Īnd finally of course you need to insert at least two object to make it works.ĭavid Wallace has provided simple to the point answer Mine is explaination of HashMap working in bit detail, So it might help others. For your case if you would like to see the equal method to be invoked, you can simulate the hash collision scenario by just making the hashcode value to be a constant. The HashMap will always compare the objects using the hashCode before the equal method. This question has been asked before in here.Īccording to the hashCode method, it states "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result." In general, whenever you create your equals method, make sure it is consistent to the returning hashcode value. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |