• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check duplicate key and value in hashmap

 
Ian Yu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic