aspose file tools*
The moose likes Beginning Java and the fly likes Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashmap" Watch "Hashmap" New topic
Author

Hashmap

Will Blanc
Greenhorn

Joined: Jul 11, 2008
Posts: 8
I understand in a hashmap you have a KEY and VALUE.

What's the most efficient way to make sure none of your values are the same as each other? That is, check to make sure you have no duplicate values?
Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
You could either call the containsValue method to see if a value is in the map already before adding new elements, or write your own subclass of HashMap that overrides the put and putAll methods to do that check automatically.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Or perhaps use the BidiMap from Apache Commons Collections.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alexander Sales
Ranch Hand

Joined: Feb 21, 2011
Posts: 89

Why not use HashSet?


OCPJP 6, OCEWCD Java EE 6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Because that does not have the key-to-value mapping.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
so what you want is a 1-to-1 cardinality?If you look here, you find that Map models the mathematical concept of a function. What you now want is called an injection (or an injective function).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashmap