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

how to remove duplicate values in hash map?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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


Creativity is nothing but Breaking Rules
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you have duplicate values, which key are you going to keep ?


[My Blog]
All roads lead to JavaRanch
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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

Joined: Jan 26, 2007
Posts: 160
What is your use case where you need to do this kind of operation
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The Last one

Is your Map ordered ? You're using a LinkedHashMap ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Check out Apache Commons Collections with its BidiMap

That's a cute name !
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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.
 
wood burning stoves
 
subject: how to remove duplicate values in hash map?