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.
source share