Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to remove duplicate values in hash map?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

How to remove duplicate values in hash map.... as we know we can apply logic and do but i want to use API methods and remove
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have duplicate values, which key are you going to keep ?
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have duplicate values, which key are you going to keep ?


The Last one as like in map when we enter duplicate keys it will keep the last duplicate key value
 
Manish Singh
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your use case where you need to do this kind of operation
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Last one

Is your Map ordered ? You're using a LinkedHashMap ?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Apache Commons Collections with its BidiMap. That's exactly what you want: a map with a one-to-one relation between keys and values instead of many-to-one.
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh.R gowda wrote:How to remove duplicate values in hash map.... as we know we can apply logic and do but i want to use API methods and remove


You cannot have multiple entries with the same key: calling put(someKey, someValue) and then put(someKey, someOtherValue) will overwrite the someKey - someValue pair.

To check the presence of a value, you have Map.containsValue(Object value), but it may not be efficient. You may be better off using 2 Maps, one for key - value, one for value - key pairs, or add/remove values to a (Hash)Set and call setForValues.contains(value) instead of map.containsValue(value). You may need to synchronize these operations to make sure your data is consistent at all times.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Apache Commons Collections with its BidiMap

That's a cute name !
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend Google Collections' BiMap instead. If only because generics have been with us for some time now, and it's annoying to deal with a library that can't handle them. And in general I find Google Collections better-engineered than most of the Apache Commons stuff I've seen. Though I haven't compared BidiMap and BiMap too carefully.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic