• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashmap

 
Will Blanc
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps use the BidiMap from Apache Commons Collections.
 
Alexander Sales
Ranch Hand
Posts: 89
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use HashSet?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that does not have the key-to-value mapping.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic