I need to get the position of a string in a List in constant time,

but do not find any such Java implementation.

Maybe is there no need for that, given current computers memory size ;-)

LinkedHashMap are interesting but do not maintain a position.(ref)

Any clue ?

2

There are 2 best solutions below

4
On

A custom implementation could keep a list and a map in parallel. The map containing the strings as keys and their list index as values. Assuming each string occurs only once.

0
On

As reminded by @Reto Höhener like said in the ref of my question to keep original insertion order index positions, one can mirror the keys in the KeySet in something like an ArrayList, keep it in sync with updates to the HashMap and use it for finding position

for a List: Filling an ordered ConcurrentMap like ConcurrentSkipListMap with a list of positions as values should do the trick. Cafeine could also do the job but I still need to look at it.

final int[] index = {0};
     Stream<ComplexObject> t = someListOfComplexObject.stream();
     ConcurrentMap<String, List<Integer>> m = 
          t.collect(Collectors.groupingBy(
               e -> e.getComplexStringElem(),
               Collectors.mapping(
                    e -> index[0]++,
                    Collectors.toList()
               ),
               ConcurrentSkipListMap::new));