This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing duplicate keys in HashMap

 
Vidya Gupta
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am having a small doubt in HashMap. Please give me some suggestions.

My Code snippet:



My Output :

key = value pairs as below,

80 : 2
80 : 4
80 : 6
10000 : 5
80 : 6
10000 : 10
80 : 6
10000 : 15
80 : 6
10000 : 20
80 : 6
10000 : 20
80 : 8



i wanted my map like this, when ever a value is changed for a particular key, that is here key is 'v3' and value is 'sum' variable, all the previous values for that key should be removed and it should be replaced by last inserted value for a particular key.
That means, the output should be like below,

80 : 8
100 : 20

Is there any way to do this.. Please give me any ideas.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you have



but I can't see some code to display the map contents ...

I think you are missing that ...

cheers :-)
 
Vidya Gupta
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit,
Thanks for the reply. i updated the code to iterate the map, here it is giving duplicate values also, but i want a key with last inserted value.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maps such as HashMap cannot contain duplicate keys, so the problem is not that you have duplicate keys in your HashMap. Something else must be the cause of the problem.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will check it once again
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the complete code ... i.e. code of the whole class ... not just this snippet
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic