I have a program, that has a
In your case, since String keys are strictly ordered,
ConcurrentSkipListMap is a way to go.
removeare as fast as
To get the next page from ConcurrentSkipListMap, call
tailMap with the last key of the previous page as an anchor and then construct an iterator or a stream from the result submap:
return map.tailMap(lastKey, false).entrySet() .stream() .limit(pageSize) .collect(Collectors.toList());
tailMap will succeed even if the anchor key is removed from the map. The iteration will proceed from the next key larger than the anchor.
If keys are not strictly ordered, or if O(1) complexity is required, then you may prefer an alternative suggestion - an open-addressed hash table traversed in the order of cell indices. However there is no such implementation among standard Java classes. The thread-safety of such maps is typically achieved by locking.