tag:blogger.com,1999:blog-3234207576654915154.post2357191812819801497..comments2023-06-04T02:18:37.275-07:00Comments on Muhammad Khojaye's Blog | Java, Cloud, Big Data: Java Hashing - Overriding hashcode and equalsMuhammad Ali Khojaye - Java, Cloud and Big Datahttp://www.blogger.com/profile/00894488663979159734noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-3234207576654915154.post-20997832945901936852017-08-02T11:49:36.077-07:002017-08-02T11:49:36.077-07:00what a clarity ...loved the way you explained !!what a clarity ...loved the way you explained !!Sapna Gangwarhttps://www.blogger.com/profile/17579722394681102585noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-18668926642395626122016-11-04T14:46:19.336-07:002016-11-04T14:46:19.336-07:00We must use all the fields in equals() methods tha...We must use all the fields in equals() methods that has been used in hashcode() method calculation. Violating this can result with different hashcode for two equal objects. However, the reverse is not mandatory and we can use the subset of fields in hashcode() method calculation (say, for improving the performance of hashcode).Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-32538546818835135822016-10-08T03:48:40.708-07:002016-10-08T03:48:40.708-07:00Hello Author, The tutorial is very good and I unde...Hello Author, The tutorial is very good and I understand now what is the use of hashcode and relation with equal. I have one question. Is it always necessary that i should use same fields in hashcode and equal function? I run an example and if I use different values in hashcode function, it will still work. can you answer this?Jai Kumarnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-24626632617855361832016-04-03T01:40:55.562-07:002016-04-03T01:40:55.562-07:00Yes, well done, it is much clearer now. As an alte...Yes, well done, it is much clearer now. As an alternative for generating hashCodes also consider java.util.Objects#hash since java 1.7.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-63408411011669344772016-03-28T05:33:22.363-07:002016-03-28T05:33:22.363-07:00Thanks, CheersThanks, CheersMuhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-19887499943086060382016-03-28T05:31:46.996-07:002016-03-28T05:31:46.996-07:00Thank you for the comment. I have updated that sec...Thank you for the comment. I have updated that section to make it more clear.<br /><br />You are on the right spot that Joshua Recipe or HashCodeBuilder cannot solve the problem for scenario 'Mutable field as a Key'. General advice is that Key should be immutable since HashMap (or HashSet) like Collection assume that an object’s hash value does not modify while it is in use as a key in the collection.<br /><br />If for any reason your key is not immutable, you need to either ensure not changing it after it has been used in a hash-based collection or you can follow the advice mentioned under the heading 'Another Example of Mutable Field as Key'.<br /><br />Cheers, Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-42043592458507081242016-03-27T04:10:47.408-07:002016-03-27T04:10:47.408-07:00In "Mutable Object As Key" the solution ...In "Mutable Object As Key" the solution of using Joshua Recipe or HashCodeBuilder won't solve the problem of not finding elements where hashCode uses mutable fields. It seems that it is offered as solution in the article but in fact it is not as tha hashCode will change after a change of name in any of the mentioned solutions as they all depend on the value of name...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-38287136401744153322016-01-21T09:45:43.726-08:002016-01-21T09:45:43.726-08:00This comment has been removed by a blog administrator.Amit Guptahttps://www.blogger.com/profile/04595196934687586126noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-67945451240445473642015-12-21T21:07:01.864-08:002015-12-21T21:07:01.864-08:00This comment has been removed by a blog administrator.dehttps://www.blogger.com/profile/03263826785097470489noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-79826565601084117492015-05-17T18:19:34.832-07:002015-05-17T18:19:34.832-07:00Thanks, nice explanationThanks, nice explanationBinh Nguyenhttps://www.blogger.com/profile/02069417158952884749noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-50883961452104861312014-12-01T14:05:40.323-08:002014-12-01T14:05:40.323-08:00Hi Can you just point out how exactly..Does this w...Hi Can you just point out how exactly..Does this work in Hash Map, existing implementation ************<br />If there are one or more objects in the bucket, then it will compare “element” with all other elements in that bucket using your defined equal() function *************<br />So in oneline if i wish to say about hash,<br />Every ideal hashkey for an object has to be unique ,considering all the instance variables of the object.<br />Also can you point where are the contracts given like immutable objects etc..in the javadocAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-3893624989343443322013-12-16T16:50:44.600-08:002013-12-16T16:50:44.600-08:00Hi,
This is one of best tutorial I read about has...Hi, <br />This is one of best tutorial I read about hashcode. I always confuse why should we override hashcode.<br /><br />I also want to add that we use prime number because It helps elements in the collection to be well distributed among the buckets. <br />The reason is that it uses the formula hashCode() % numberOfBuckets to compute the bucket where the object will store and prime number is not a multiple of the number of buckets (which is a power of 2) therefore, hashCode() % numberOfBuckets will not repeat quickly to the same number bucket. In short, prime number will helps the objects to be distributed better over the buckets.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-86609802631990666692013-12-09T12:56:37.682-08:002013-12-09T12:56:37.682-08:00Generally, its not easy to answer because it depen...Generally, its not easy to answer because it depends on the scenario for which you are implementing the hashCode() method and also on what equality means for objects of that class. A good hashCode implementation returns unique hash codes for different objects, however, it is impossible to guarantee that hashcodes will be unique for each object. <br /><br />A good general guideline-purpose guideline has been explained in heading "Overriding hashCode Method" heading.<br />I hope it helps.Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-32001597026396413602013-12-09T12:39:03.622-08:002013-12-09T12:39:03.622-08:00How do we know that we have written best implement...How do we know that we have written best implementation of hashcode method for a collection?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-84976176322323499292013-03-17T03:24:57.844-07:002013-03-17T03:24:57.844-07:00The current implementation is just using the custo...The current implementation is just using the custom inner class named Entry where hashcode saved. Once retrieve the hashcode (and collision occur), the linkedin of object maintain. Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-43869605135415822052013-03-07T20:35:44.346-08:002013-03-07T20:35:44.346-08:00Which data structures is been used to store the ha...Which data structures is been used to store the hashcode in the buckets ?Any idea<br /><br />My perspective is linked list. but any suggestions from your endgauravhttps://www.blogger.com/profile/02382301392533656128noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-90808411773632738412012-08-26T04:33:34.637-07:002012-08-26T04:33:34.637-07:00I am glad to know that it was helpful for youI am glad to know that it was helpful for youMuhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-14915511882423542372012-08-16T11:07:33.722-07:002012-08-16T11:07:33.722-07:00Nice explanation, I used your article to convince ...Nice explanation, I used your article to convince my manager who isn't so much technical about overriding hashcode in all the POJOs we use.Chiphttp://ww.chip.menoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-61640640843273150262011-06-13T10:19:40.003-07:002011-06-13T10:19:40.003-07:00Consider below sample example where we do not over...Consider below sample example where we do not override hashcode(), and therefore, the object fail to get. <br /><br />public class Test {<br /> public static void main(String[] args) {<br /><br /> Map employees <br /> = new HashMap();<br /> employees.put(new <br /> Employee("Khojaye", 25), "Khojaye");<br /><br /> String employeeName = employees.get(new <br /> Employee("Khojaye", 25));<br /> <br /> System.out.println(employeeName); <br /> // this will print null<br /> System.out.println(new <br /> Employee("Khojaye", 25).equals(new <br /> Employee("Khojaye", 25))); <br /> // this is true because of the<br /> // overriden equals()<br /> }<br />}<br /><br />class Employee {<br /> private String name;<br /> private int age;<br /><br /> public Employee(String name, int age) {<br /> this.name = name;<br /> this.age = age;<br /> }<br /><br /> public String getName() {<br /><br /> return name;<br /> }<br /><br /> public int getAge() {<br /> return age;<br /> }<br /><br /> @Override<br /> public boolean equals(Object obj) {<br /> if (obj == this) {<br /> return true;<br /> }<br /><br /> if (!(obj instanceof Employee)) <br /> {<br /> return false;<br /> }<br /><br /> Employee person = (Employee) <br /> obj;<br /><br /> return person.getName()<br /> .equals(this.name)<br /> && <br /> person.getAge() == <br /> this.age;<br /> }<br /><br /> // didn't override hashCode!!!<br />}<br /><br />Please let me know, If there is any thing still unclear.Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-25953843858449818442011-06-12T07:16:13.011-07:002011-06-12T07:16:13.011-07:00Objects that are equal but return different hashCo...Objects that are equal but return different hashCodes: For HashSet, this would mean duplicates across the set, however, I am not able to figure out how this will affect HashMap. Can you post an example?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-8883961821497724032010-10-31T04:56:43.623-07:002010-10-31T04:56:43.623-07:00Thank you so much. I'm glad you like it :)Thank you so much. I'm glad you like it :)Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-29635095802834825402010-10-09T12:47:24.997-07:002010-10-09T12:47:24.997-07:00Hi Khojaye, Thanks for posting really this is frui...Hi Khojaye, Thanks for posting really this is fruitful for me..i m frm india..thanks bro :)Unknownhttps://www.blogger.com/profile/10417647036684314122noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-32221004928102647932010-03-07T08:15:54.380-08:002010-03-07T08:15:54.380-08:00@jmagiera
Yes, I agree. Equal() should be used ins...@jmagiera<br />Yes, I agree. Equal() should be used instead.<br /><br />Cheers!Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-13802189113047292402010-03-03T05:33:55.451-08:002010-03-03T05:33:55.451-08:00Please don't implement String equality with ==...Please don't implement String equality with ==. Use equals(). Not all Strings are interned, only literals.<br /><br />Illustration:<br /><br />String one = "test";<br />String two = new String("test");<br />String three = new String("test").intern();<br />System.out.println("equal? " + (one == two));<br />System.out.println("equal? " + (one == three));Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3234207576654915154.post-81983029444013361672010-03-01T21:57:17.884-08:002010-03-01T21:57:17.884-08:00I am so glad, this small tutorial work for you guy...I am so glad, this small tutorial work for you guyz. Thanks for the comments.<br /><br />Cheers!!Muhammad Ali Khojaye - Java, Cloud and Big Datahttps://www.blogger.com/profile/00894488663979159734noreply@blogger.com