GeeCON Prague 2014*
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


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: 4397
    
    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.


 
GeeCON Prague 2014
 
subject: check duplicate key and value in hashmap