• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap with Integer as a key

 
Vadym Baranenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
during the reading of Java documentation I found one interesting thing about the collections. Wrapper classes do not override equals method. So it sepouseto mean that it's impossible to use them as a keys to map. I found this interesting because I use wrappers before and thay seems work fine. So I wrote simple test code:


And now I feel like a fool
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you happened to check the source of the Integer.java wrapper class, you can find that the equals method has been overridden to check that both the Integer objects have same int value.
 
Vadym Baranenko
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:If you happened to check the source of the Integer.java wrapper class, you can find that the equals method has been overridden to check that both the Integer objects have same int value.

Yes, you are right. After posting this topic I go to source code and found this line"


It was mistake in the book i reading now and a mistake in my code. Double mistake confused me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic