tag:blogger.com,1999:blog-8712770457197348465.post3406296824158647273..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: How does Java HashMap or LinkedHahsMap handles collisions?javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-8712770457197348465.post-46670462973091133362018-05-29T03:48:40.529-07:002018-05-29T03:48:40.529-07:00Without using comparable class as key in HashMap, ...Without using comparable class as key in HashMap, it convert Linked List to Tree Node. Still I don't know, how it convert it into tree. What is the comparison ground when it convert linked list to tree.Anonymoushttps://www.blogger.com/profile/06040681422807733243noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85600724473687042252017-09-18T00:47:57.182-07:002017-09-18T00:47:57.182-07:00What is the difference between add() and set() met...What is the difference between add() and set() method in ArrayListDeepanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20251576069151383182016-01-24T05:37:13.933-08:002016-01-24T05:37:13.933-08:00@Javin
I was referring to @Saral's explanation...@Javin<br />I was referring to @Saral's explanation above, which read as<br />"If the two hash codes are different but ended up in the same bucket then one of them is considered bigger and goes to the right of the tree and other one to the left. But when both the hash codes are equal, then HashMap assumes that the keys are comparable and the key compares to determine the direction so Parthhttps://www.blogger.com/profile/14857867985563454912noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78498460973688433902016-01-15T22:17:19.316-08:002016-01-15T22:17:19.316-08:00@Parth, There is no requirement for key to impleme...@Parth, There is no requirement for key to implement Comparable in-order to be stored in HashMap, only equals() and hashCode() is required. Comparable is required in case of Sorted collection e.g. TreeMap or TreeSet. <br /><br />Coming back to your question if key doesn't follow equals() and hashCode() contract than Map' invariant will not hold e.g. not allowing duplicate values and javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66173180600411059232016-01-14T20:43:25.959-08:002016-01-14T20:43:25.959-08:00So how it will work when key element is not implem...So how it will work when key element is not implementing comparable?<br />Will it fail run time?Parthhttps://www.blogger.com/profile/14857867985563454912noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38964317769767234382016-01-09T15:56:42.126-08:002016-01-09T15:56:42.126-08:00@Pritiviraj, LinkedHashMap's iteration order w...@Pritiviraj, LinkedHashMap's iteration order will not change because its a documented behavior and the class guaranteed that. On the other Iteration order of HashMap and Hashtable are not guaranteed, it's undocumented behavior. <br /><br />Due to legacy nature of Hashtable it's not touched and its iteration order will remain same, as many Java application rely on that, but for HashMapjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-672727208873201872016-01-09T08:58:06.077-08:002016-01-09T08:58:06.077-08:00You mentioned that "By switching from linked ...You mentioned that "By switching from linked list to balanced tree for handling collision, the iteration order of HashMap will change."<br />As per my understanding LinkedHashMap maintains order. What happens in case of LinkedHashMap, when switching from linked list to balanced tree for handling collision occurs?Prithvirajhttps://www.blogger.com/profile/13096669674298889545noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44029530398840211712016-01-08T20:16:03.167-08:002016-01-08T20:16:03.167-08:00@Anonymous, very good question and @Saral, equally...@Anonymous, very good question and @Saral, equally good answer. The hreshold switch from linked list to balanced tree as data structure is defined in java.util.HashMap class as TREEIFY_THRESHOLD = 8. You can check the code, especially put() method which does that conversion. You can open a class from JDK in Eclipse by using shortcut Ctrl + T and in Netbeans by using Ctrl + O open a type. For javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28353324734827255892016-01-08T07:25:15.547-08:002016-01-08T07:25:15.547-08:00In an HashMap the key is an object, that contains ...In an HashMap the key is an object, that contains hashCode() and equals(Object) methods.<br /><br />When you insert a new entry on the Map, it checks whether the hashCode is already known. Then, it will iterate through all objects with this hashcode, and test their equality with .equals(). If an equal object is found, the new value replace the old one. If not, it will create a new entry in the SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10465283531734085972016-01-07T21:25:56.473-08:002016-01-07T21:25:56.473-08:00What is the threshold value to exceed for switchin...What is the threshold value to exceed for switching from linked list to balanced tree as data structureAnonymousnoreply@blogger.com