Which java collections (and maps) can be sorted by last access

I know that LinkedHashMap provides a constructor where you can specify whether the card should be sorted by access order, thereby effectively providing an LRU implementation. Can you tell me which (and if) other collections and maps from the zoo of large collections provide this feature?

+4
source share
2 answers

I do not think that I fully understand the question, but maybe you want to look at the LRUMap implementation of the General framework of collections .

+4
source

I do not think that such Collections or Maps exist (but I also heard about this constructor for the first time just now). I checked Guava , but I don't think they have a solution.

But I think this can be easily achieved using a decorator pattern. Write a delegate object that implements the required interface and delegates all methods to the internal object. Your shell also contains a LinkedHashSet / LinkedHashMap (depending on whether you are dealing with a collection or map) that registers access to data.

Now your iterator () / entrySet () methods provide a view that was first supported by LinkedHashSet / Map and then the rest of the data (or vice versa if you want to change the access order).

I would implement it using wrapper methods, such as those contained in the Collections class.

eg.

Map<String,String> map = CollectionUtils.viewMapByAccessOrder( new HashMap<String,String>()); List<String> list = CollectionUtils.viewListByAccessOrder( new ArrayList<String>()); 

This may be functionality that makes sense for a wider audience. I would like to consider a function request in a Guava project.

+1
source

All Articles