wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 41155
    
  45
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: 19656
    
  18

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: 19656
    
  18

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

Joined: Oct 13, 2005
Posts: 38075
    
  22
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
 
Similar Threads
Max,Min
How can I upload multiple files using html:file
Struts Iterator best Object usage
Sorting HashMap by values
why hashmap allows only 1 null value..?