File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

check duplicate key and value in hashmap

Ian Yu

Joined: Feb 09, 2011
Posts: 1

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

Joined: Apr 06, 2010
Posts: 4543

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.

I agree. Here's the link:
subject: check duplicate key and value in hashmap
It's not a secret anymore!