I need an ordered data structure with O(1) indexOf operation. I store object pointers in the data structure. Any ideas? Some sort of LinkedHashMap?
See what "indexOf" means: List.indexOf(Object)
I need an ordered data structure with O(1) indexOf operation. I store object pointers in the data structure. Any ideas? Some sort of LinkedHashMap?
See what "indexOf" means: List.indexOf(Object)
This question is ambiguous to begin with.
indexOf(..) operation.indexOf(..) the only responsibility of the collection. Simply put, one way to do this would be to maintain a that would index each Object or keys with the list of indices.
HashMap<Object, List<Integer>>
Again, this is vague, probably would help if you specify the exact nature of problem you're trying to solve.
If you use a skip list or a balanced tree, you can get O(log n) for insert and indexOf.
If you maintain a sorted List<Object> to store the items you want to track, along with a HashMap<Object, Integer> to store the starting position of each item, you can get O(1) indexOf in exchange for O(n) insert.
I haven't thought about it deeply, but I don't think it's possible to get O(1) insert and O(log n) indexOf.
try using PriorityQueue instead to make it oredered...