Is there anyway I can print out the Keys with the values that had collisions ? If a key has two values I want to be able to print out those values how can I do that I am inside the hash map class and making modification to it.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a
HashMap, a key cannot have two values. If you callmap.put(key,value)with an existingkey, the old value is removed from the map, and is returned byput().One way to have multiple values per key is by using
HashMap<K,Collection<V>>. This automatically provides the functionality you want, since you can simply examine the contents of the value collection after you’ve added the new element to it.There are also third-party classes that provide this functionality, such as
MultiValueMap.edit:
If you’re talking about multiple keys ending up in the same bucket, then you need to modify
HashMap‘sput()method:(Add your code where the
TODOline is.)You’ll need to make similar changes to
putForNullKey()and other related methods, such asputForCreate().