• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to override HashCode when your class has only String variables  RSS feed

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
I have seen lots of example to override hashcode and equals. classes contain atleast one primitive data type using which they override hashcode that return int.
Now suppose i have class which has only String as variable how can i override hashcode for the class.



Thank You
 
Brett Spell
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this would work:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Spell wrote:Something like this would work:


Well, the method also needs to adhere to the equals() / hashCode() contract, so it may be different depending on what is actually the definition of equality according to the class.

Henry
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More details here. Why reinvent the wheel?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry points out, you would have to use all 7 fields in equals() in my example, too.
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i hope below code is correct

 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Well, the method also needs to adhere to the equals() / hashCode() contract, so it may be different depending on what is actually the definition of equality according to the class.


He specifically asked, "how can i override hashcode for the class" and his other comments seem to indicate that he understands that he also needs to override equals() and its relationship to the hash code.

There are many other things that can be said when answering a question like this but I chose to address the question that was asked. Personally I find "answers" that don't answer the question asked -- or that do answer questions that weren't asked -- to be less helpful than those that simply address what the poster wanted to know, so I try to respond accordingly.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gajendra Kangokar wrote:i hope below code is correct


Yes, other than a missing closing parenthesis and return statement (which the compiler will bring to your attention) that'll work. If your code only needs to run on Java 7 or later and there's no requirement that you calculate the hash code yourself, though, using Objects.hash() as Campbell suggested is a better option.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Spell wrote:
He specifically asked, "how can i override hashcode for the class" and his other comments seem to indicate that he understands that he also needs to override equals() and its relationship to the hash code.

There are many other things that can be said when answering a question like this but I chose to address the question that was asked. Personally I find "answers" that don't answer the question asked -- or that do answer questions that weren't asked -- to be less helpful than those that simply address what the poster wanted to know, so I try to respond accordingly.


I was merely qualifying your response. I did not mean to imply that your response was incorrect, did not address the question, or was not helpful. Sorry if it came across that way -- it was not my intention.

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wonder what is 17 and 31 in the code?
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like 17 is assumed value.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 7 there's a new class Objects which has several useful utility functions. In your case your hashCode() method would look like this:



... never mind, I see Campbell posted that about 6 hours ago.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to double check OP using instanceOf in equals() method and i think we should use instanceOf only for final classes..

As per article http://www.javaranch.com/journal/2002/10/equalhash.html getClass() should be used in place of instanceOf. instanceOf will not return
false if the argument is subclass of the class which we compare with. This violates the symmetry requirement.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!