• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interview question  RSS feed

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When hashCode() is override, but not equals(), what will happen when object is used as the key in HashMap?
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think will happen? Will it find the bucket? If no, why not? If so, what will it do with the objects in that bucket?
 
S. Palanigounder
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If hashCode is overwritten correctly, nothing will happen. The object can be the correct key used in HashMap.
 
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it needs both to work properly. As shown below, it calls the hashCode method when adding items to the Map, and calls both methods when retrieving items.
Prints the output:
 
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
S. Palanigounder wrote:If hashCode is overwritten correctly, nothing will happen. The object can be the correct key used in HashMap.


Michael Angstadt wrote:No, it needs both to work properly.



I think this depends on what "overwritten correctly" and "work properly" means. If "overwritten correctly" just means that the hashCode / equals contract is not violated, then the object can be used as a key in a hashmap. And if "work properly" means that equality is based on instance, and not based on the value, then it should work properly. Of course, that could also have been achieved by not overriding hashCode() in the first place.

Henry
 
S. Palanigounder
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,

If you do not overwrite the equals method, which means using the default equals method, your code works fine.
The default equals method uses the bit-wise comparison.
 
Michael Angstadt
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:And if "work properly" means that equality is based on instance, and not based on the value, then it should work properly

Thanks Henry, yes, that's what I meant. I should have been more specific.

FYI: In my example, if you instead store the first instance of "new Wrapper("one")" in a variable and pass that to the Map.get() method, then you don't need the hashCode or the equals method.

S. Palanigounder wrote:The default equals method uses the bit-wise comparison.

No it doesn't...it compares the reference values for equality, as in:

 
S. Palanigounder
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Angstadt wrote:

S. Palanigounder wrote:The default equals method uses the bit-wise comparison.

No it doesn't...it compares the reference values for equality, as in:



What is the difference between "==" operator and "equals" method?

For example, String a=”12”; String b=”12”; String c= new String(“12”); They all have same content. But are they "==" or "equals"?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? What do you know about the behaviour of identical String literals, or copy constructors?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, many things could happen but having different scenarios.


Having hashCode well implemented means that the Object will find the correct bucket where this Object is in the Map buckets.

If you are using the same reference it will be found:


this will print "Hello World"

If not it won't be found:



this will print null


but if you had had equals overriden and well implemented it should return "Hello World"

 
S. Palanigounder
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulises, Can you provide the override implementation for hashCode and equals that works?
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look in books like Thinking in Java by Bruce Eckels, Effective Java by Joshua Bloch.
Please search for Angelika Langer's pages about the Java equals method.
Please search these fora because this question comes up frequently.
 
Ulises Pulido
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S. Palanigounder wrote:Ulises, Can you provide the override implementation for hashCode and equals that works?


Well there is no equals and hascode implementation that can fit for all the objects.

Depending on the class you have created is the implementation you can do. What I can give you are the rules for the implementation.

If x == y then it must be that x.hashcode == y.hashcode
If x != y then x.hashcode might be == or != than y.hashcode
If x.equals(y) is true then it must be that x.hashcode == y.hashcode.
If !x.equals(y) then x.hashcode might be == or != than y.hashcode.
if x.equals(y) and y.equals(z) then x.equals(z). The same goes for hashcode.

Once you have implemented both methods and they behave as the rules stated before then you can say that you have a correct implementation of both methods.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!