There is no Java (standard) collection type with that property.
The indexOf(Object)
method is declared in List
, and there are no List
implementations that provide better than O(N)
lookup.
There are other collection types that provide O(logN)
or O(1)
lookup, but they do not return a position (and index) like indexOf(Object)
does. (HashMap
and HashSet
provide respectively get(key)
and contains(object)
that are O(1)
.)
If there is nothing, can anyone help me out to deduce the reason why such has not been formed? Any particular reason?
Because a data structure that did provide such functionality would be complicated, and (probably) slower and/or combine the drawbacks of both an ArrayList
and a HashMap
.
I can't think of a good way to implement a list with fast lookup; i.e. one that didn't have serious drawbacks. And if there was a good way, I would have expected the Java team to know about it ... and to have included it in the Java SE library. This is similar to why there are no general purpose concurrent list types.
I am not saying that it is impossible. I'm saying that if it is possible, nobody has discovered a way to do it. AFAIK.
Designing an general purpose collection type involves compromise. When you optimize for one mode of access, you will often de-optimize for another.