File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Storm Applied this week in the Other Open Source APIs 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
Rancher

Joined: Mar 22, 2005
Posts: 42955
    
  73
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19972
    
  25

Or perhaps use the BidiMap from Apache Commons Collections.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 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: 19972
    
  25

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

Joined: Oct 13, 2005
Posts: 42026
    
  31
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