• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to remove duplicate values in hash map?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have duplicate values, which key are you going to keep ?
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your use case where you need to do this kind of operation
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The Last one


Is your Map ordered ? You're using a LinkedHashMap ?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check out Apache Commons Collections with its BidiMap


That's a cute name !
 
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic