• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()

 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also have to override hashCode() for T
 
vinayak shete
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet,

Have a cow for your effort and for sharing what you learned.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic