| 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 = )
|
 |
Faraz Kadri
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: 3786
|
|
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.
|
 |
 |
|
|
subject: check duplicate key and value in hashmap
|
|
|