HashMap: One Key, multiple Values
How I can get the third value for the first key in this map? Is this possible?
Libraries exist to do this, but the simplest plain Java way is to create a Map
of List
like this:
Map<Object,ArrayList<Object>> multiMap = new HashMap<>();
It sounds like you're looking for a multimap. Guava has various Multimap
implementations, usually created via the Multimaps
class.
I would suggest that using that implementation is likely to be simpler than rolling your own, working out what the API should look like, carefully checking for an existing list when adding a value etc. If your situation has a particular aversion to third party libraries it may be worth doing that, but otherwise Guava is a fabulous library which will probably help you with other code too :)