A HashMap
has constant access time but does not allow duplicates. An ArrayList
allows duplicates but does not have constant access time.
Is there a data structure in java which allows constant access time and allows duplicates?
I know I could make my own HashMap
which allows duplicates, but I want to use an already existing data structure.
Thank you in advance.
You could use a Bag from Eclipse Collections, a Multiset from Google Guava, or a Bag from Apache Commons Collections. A
Bag
is basically aMap<Key, Integer>
which behaves like a Collection.All three libraries have Multimaps as well. A
Multimap
is basically aMap<Key, Collection<V>>
, where a call to put results in adding to theCollection<V>
instead of replacing the value at that key. There are different types ofMultimap
(List
,Set
,Bag
, etc.).Note: I am a committer for Eclipse Collections