This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 42958
    
  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: 20136
    
  30

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: 20136
    
  30

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

Joined: Oct 13, 2005
Posts: 44561
    
  34
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
 
It's not a secret anymore!