Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?
Please do help me, also by telling (if there is no way) any other way to implement the storage of three values with one as the key?
Is it possible for us to implement a HashMap with one key and two values. Just as HashMap?
Please do help me, also by telling (if there is no way) any other way to implement the storage of three values with one as the key?
Can be done using an identityHashMap, subjected to the condition that the keys comparison will be done by == operator and not equals().
Another nice choice is to use MultiValuedMap from Apache Commons. Take a look at the All Known Implementing Classes at the top of the page for specialized implementations.
Example:
could be replaced with
So,
would result in collection
coll
containing "A", "B", and "C".Take a look at
Multimap
from the guava-libraries and its implementation -HashMultimap
You can do it implicitly.
This is very simple and effective. If you want values of diferent classes instead, you can do the following:
I could not post a reply on Paul's comment so I am creating new comment for Vidhya here:
Wrapper will be a
SuperClass
for the two classes which we want to store as a value.and inside wrapper class, we can put the associations as the instance variable objects for the two class objects.
e.g.
and in HashMap we can put in this way,
WrapperObj will have class variables:
class1Obj, class2Obj