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.
ArrayList#getandArrayList#setare actually constant time, as well as a few other functions. Read the documentation, second paragraph of the class documentation:Your next option would be a multimap. This is a map that stores items in a key/collection manner. The collection holds the values, so a single key map to multiple values. You can look into Apache Common's
MultiMapto see if they have an implementation that works for you. Or you could always create your own, simply by defining a collection as the value: