I want to create a HashMap
where each Key
could have multiple Value
s. For example, the key
umbrella could have value
s of red, black, and green. I have heard that the buckets in a Hashtable
could be LinkedList
s, ArrayList
s, etc. How could I implement a bucket as an ArrayList
so that I would be able to add items that match the key
to the end of the list?
I want to have a something like Map<Key, Value>
. If the Key
exists, the Value
will be added to the list of current Value
s.
You should use
Map<K, List<V>> map = new HashMap<>();
Instead of
map.put(k, v)
, you will do something like this: