File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Removing duplicate keys in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing duplicate keys in HashMap" Watch "Removing duplicate keys in HashMap" New topic
Author

Removing duplicate keys in HashMap

Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98
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

Joined: Jun 08, 2010
Posts: 384

In the code you have



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

I think you are missing that ...

cheers :-)


http://plainoldjavaobject.blogspot.in
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98
i will check it once again
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

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
 
subject: Removing duplicate keys in HashMap