• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Verification of concept HashMap uses .equeals() method on key object during put operation.  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was testing the concept mentioned in subject line. As I see this seems correct with Wrapper class but not with user-defined class.
My understanding is: While put operation HashMap checks keys with .equeals() (content of key object) method and if it is same it replaces existing Object for that key.
But below program is not confirming this with user-defined class's object.
Please, correct if I am missing something :

Test Code and its Output :



Output:

>java Test
With Wrapper classes .equals() method working. :
{20=b, 10=a_new}

With user defined classes .equals() method is not working. :
{T@1=a_new, T@1=b, T@1=a}

 
author & internet detective
Posts: 39283
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinayak,
Welcome to CodeRanch!

The equals() method takes Object as a parameter; not the type you are working on. You've implemented a similar method, but it doesn't actually override equals() so HashMap doesn't call it.

Tip: Add @Override to your method. This will be caught by the compiler as not actually overriding equals()

 
Bartender
Posts: 5825
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have to override hashCode() for T
 
vinayak shete
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Jeanne Boyarsky and Carey Brown.

@Carey Brown. : I have already Override hashCode() method (will return same hash code - as 1 for each object.)
@Jeanne Boyarsky : I have tried with @Override annotation. When I put @Override annotation getting compile time error :

>javac Test.java
Test.java:41: error: method does not override or implement a method from a supertype
       @Override
       ^
1 error

 
Jeanne Boyarsky
author & internet detective
Posts: 39283
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vinayak shete wrote:@Jeanne Boyarsky : I have tried with @Override annotation. When I put @Override annotation getting compile time error :


Right. Because I posted it with the compiler error so you would get to see it and fix it. You need to change the type of the parameter to Object
 
vinayak shete
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Jeanne Boyarsky

I got the point now. It was due to I didnt Overriden .equals() method.

Below change did the task.


Output is as expected:

>java Test
With Wrapper classes .equals() method working. :
{20=b, 10=a_new}

With user defined classes .equals() method. :
{T@1=b, T@1=a_new}

 
Jeanne Boyarsky
author & internet detective
Posts: 39283
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Also, see our Equals/hash code FAQ for a better implementing of equals(). Yours throws an exception under certain circumstances.
 
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind what Carey mentioned about overriding hashCode() as well. Read and understand the API documentation for Object.equals() and Object.hashCode()
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vinayak shete wrote:. . .  hashCode() method (will return same hash code - as 1 for each object.) . . .

That is a very effective way to make your hash map execute with all the speed and athleticism of a dying snail. Write a hashCode method that computes a hash code for each instance; Carey has already shown you a possible implementation. If you are using an immutable type as the “K” in your map (as you should anyway), you may be able to cache the hash code.
 
Saloon Keeper
Posts: 3252
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is new to me!

I always wondered why we had to override the equals method with Object as parameter, instead of having an equals method with the new class as parameter, so overloading instead of overwriting.

But after experimenting, indeed, the Objects.equals method uses the equals method of Object, never knew this.

I wrote a small program to test this, more for myself to get a better understanding of this, but maybe it is illustrative for other people as well.

If you comment out the @Override part, you indeed get other outcomes
 
Junilu Lacar
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet,

Have a cow for your effort and for sharing what you learned.
 
Saloon Keeper
Posts: 10211
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, from me as well.

Piet Souris wrote:If you comment out the @Override part, you indeed get other outcomes


Can you clarify this? @Override doesn't affect the outcome of a program.
 
Piet Souris
Saloon Keeper
Posts: 3252
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:(...)
Can you clarify this? @Override doesn't affect the outcome of a program.


Sorry for the confusion! I meant this:
 
You have to be odd to be #1 - Seuss. An odd little ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!