tag:blogger.com,1999:blog-8712770457197348465.post377043274825848363..comments2024-03-29T03:17:04.645-07:00Comments on Javarevisited: How to override hashcode in Java example - Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-8712770457197348465.post-2126075263301550902015-11-02T18:57:55.885-08:002015-11-02T18:57:55.885-08:00Hello Gauri,, No, compiler will not give any erro...Hello Gauri,, No, compiler will not give any error. If two objects are equal using equals() method then their hashCode() methods also should return same integer value. Vice versa is not true.Anonymoushttps://www.blogger.com/profile/06223382815280014703noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71767536528183959352013-06-30T21:14:50.825-07:002013-06-30T21:14:50.825-07:00Hello Javin, What is the purpose of hashCode and w...Hello Javin, What is the purpose of hashCode and what is the relationship between equals and hashCode in Java, does compiler complains if we override one but not other? if not, why not?Gaurinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25549818990848571442013-06-19T07:56:02.631-07:002013-06-19T07:56:02.631-07:00@Venkat Sarma
Probably you did override hashcode()...@Venkat Sarma<br />Probably you did override hashcode() without overriding equals. So in that case all your objects were stored in the same bucket, which leads to O(n) lookup instead of O(1) which is normally expected in a HashMap.Kenhttps://www.blogger.com/profile/08028651449316484899noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85130447688141548252013-06-19T07:49:35.780-07:002013-06-19T07:49:35.780-07:00@Naveen Vanjani and saikinranputta
You need to ove...@Naveen Vanjani and saikinranputta<br />You need to override equals and hashcode if you want colletions based on hashes to work well, like hashset and hashmap.<br /><br />If you put a Person object with the same 'name' and 'surname' fields inside a HashSet many times you'd expect the set to only have it once, but as their hashcodes wouldn't be the same without overriding Kenhttps://www.blogger.com/profile/08028651449316484899noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35251247787964544002013-04-24T07:17:43.530-07:002013-04-24T07:17:43.530-07:00Hi Nice article,
In one of my interviews asked m...Hi Nice article,<br /> In one of my interviews asked me a question that what happens when hashcode of two key objects are equals? tell me what happens while put and get methods. I couldn't answer this question. I came back and written a simple program like for all custom objects it returns the same hashcode, tried adding them to hashmap. But it is working fine, I mean to say I could see theVenkat Sarmahttps://www.blogger.com/profile/02645783476021979849noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65461902948676146122013-04-14T19:55:52.190-07:002013-04-14T19:55:52.190-07:00Hi,
this is an amazing article...
It will be great...Hi,<br />this is an amazing article...<br />It will be great to know more about the has code as in why we need a hash code at all.. how a hash code is used by jvm ?<br /><br />hash code comes into picture when there is a collision, but I am not sure if that is it, the use of hash code.<br /><br />Thanks in advance<br />Naveen VanjaniAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55989925777134115532013-02-19T07:38:27.695-08:002013-02-19T07:38:27.695-08:00Why do you need hashcode? I mean what is the pract...Why do you need hashcode? I mean what is the practical application of it's? <br />Overriding equals() and hashcode()..!!? How/why would you use that?!<br />Please be specific.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22112488154726878782013-01-07T05:37:51.243-08:002013-01-07T05:37:51.243-08:00I get an exception when I’m trying to start tomcat...I get an exception when I’m trying to start tomcat (in thread main java.lang.NoClassDefFoundError: Anonymoushttps://www.blogger.com/profile/13803056408299207262noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82263052283392480412012-07-18T23:10:12.412-07:002012-07-18T23:10:12.412-07:00@javin am not getting auto-generate hashcode and e...@javin am not getting auto-generate hashcode and equals option in netbeans 7.2.<br />help pleas??genexhttps://www.blogger.com/profile/04048345451470573346noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33388959821235216302012-07-12T23:42:00.456-07:002012-07-12T23:42:00.456-07:00Use IDE to generate code for equals and hashCode, ...Use IDE to generate code for equals and hashCode, there is no point doing it manually. you might want to highlight the importance of hashCode when an object is used as key in HashMap. Though its not a requirement that unequal object should have unequal or unique hashCode, you should try for that.Sunidhinoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55215971317192806272011-12-01T23:59:17.345-08:002011-12-01T23:59:17.345-08:00hashcode in java example is very good but you didn...hashcode in java example is very good but you didn't tell us how to write hashcode for immutable class, will be same of there would be any difference. also what are performance consideration in java while overriding hashcode in java ? What happend if I return same hashcode for every object ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2854288842740714612011-10-29T21:54:18.397-07:002011-10-29T21:54:18.397-07:00Can we make hashCode method synchronized in Java, ...Can we make hashCode method synchronized in Java, What are the consequences of using synchronized keyword with equals and hashCode method ? Also is there any way we can still do in memory comparison using native equals method in JavaAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31002813763262027212011-10-07T23:28:25.588-07:002011-10-07T23:28:25.588-07:00@OnlySoftware , thanks for your suggestion I tried...@OnlySoftware , thanks for your suggestion I tried both EqualsBuilder and HashCodeBuilder and found them easy to use and probably best option, though it introduce additional dependency on Apache commons but in the end its worth to include Apache commons which has lots of utility.Javin @ polymorphism in javahttp://javarevisited.blogspot.com/2011/08/what-is-polymorphism-in-java-example.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-89290243299322148002011-10-03T07:31:13.408-07:002011-10-03T07:31:13.408-07:00Excellent tips. But I would like to say that JDK7 ...Excellent tips. But I would like to say that JDK7 has come up with awesome Objects class which can easily generate hashcodes for classes.<br /><a href="http://extreme-java.com/string-equals-hashcode-hashmap/" rel="nofollow">How hashcode behaves in String</a>Sandeephttps://www.blogger.com/profile/14459583259453333684noreply@blogger.com