This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashMap with Integer as a key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap with Integer as a key" Watch "HashMap with Integer as a key" New topic
Author

HashMap with Integer as a key

Vadym Baranenko
Greenhorn

Joined: Oct 06, 2010
Posts: 18
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

Joined: Sep 08, 2007
Posts: 3054
    
  33

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.


Mohamed Sanaulla | My Blog
Vadym Baranenko
Greenhorn

Joined: Oct 06, 2010
Posts: 18
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap with Integer as a key
 
Similar Threads
Maps
problem while reusing a List object
Retrieve values using hashmap
TreeMap containsKey issue
Printing HashMap!