This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes check duplicate key and value in 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 » Java in General
Bookmark "check duplicate key and value in hashmap" Watch "check duplicate key and value in hashmap" New topic
Author

check duplicate key and value in hashmap

Ian Yu
Greenhorn

Joined: Feb 09, 2011
Posts: 1
hello,

i was wondering how you would check for a duplicate item that has both the same key and value.

ie, if you do like

checkDuplicates.put("David", 123)
is there a way to check a string that has exactly that key?

if i map another "David" with 555, that's totally fine.

but im having trouble trying to figure out a way to check if there is another item that has "David" with 123.

any pseudocode would be great = )
thank you = )
Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
You could do a check using the Map's containsKey() method to see if the key is already present in your Map.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

A hash map can only store one value for each key. So if you do add another "David" with value 555 it will overwrite the original value.

If you want to check whether a particular key/value pair is contained, just extract the value (if any) and compare it. E.g.


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: check duplicate key and value in hashmap
 
Similar Threads
Ordered List
elements from hashtable by index
How to remove object from Vector?
Hashmap
one to many relation error