Data structure with fast index?

I need an ordered data structure with an O (1) indexOf operation. I store object pointers in a data structure. Any ideas? Some kind of LinkedHashMap?

See what "indexOf" means: List.indexOf(Object)

+5
source share
5 answers

This question is ambiguous to begin with.

  • It would be nice if you could define what you mean by operation indexOf(..).
  • What objects are you going to store in the collection?
  • Discovers the indexOf(..)sole responsibility of the collection.

, - , Object .

HashMap<Object, List<Integer>>

, , , , , .

+6

, SortedMap, TreeMap . log(n) , , ( ).

+2

, O (log n) insert indexOf.

List<Object> , , HashMap<Object, Integer> , O (1) indexOf O ( n) insert.

I did not think deeply about this, but I do not think that it is possible to get an O (1) insert and an O (log n) indexOf.

+1
source

try using PriorityQueue instead to make it ...

0
source

Save the object as a key and value in a HashMap. I assume that you want to get the object in the end.

0
source

All Articles