Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Duplicate Keys

 
Willie Toma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are duplicate keys in the following statement? Is it the name of the class?
"An interface that ensures that implementing classes cannot contain duplicate keys."
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's refering to a Map interface. The keys are used to identify an object in a collection. Example impllementations are hashTable and Hashmap. They store key/value pairs, and the keys can not be duplicate.

Bosun
 
NILESH PATEL
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If one wants to create a duplicate key with different values then, what class/interface is the best to use, Any Ideas?
Thanks
Nilesh
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use Map.
Yes, seriously. If you have a duplicate key with a different value, how would you know which value to return for that key? You can't know. All you can do is return all the values for that key, which corresponds to a Map that maps the key to a Collection of values. The key => Collection map is once again unique.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic