Java – iteratively find a map entry at the index?
•
Java
I have a LinkedHashMap I want to get foo in index n Is there a better way to do this than iteration?
int target = N; int index = 0; for (Map.Entry<String,Foo> it : foos.entrySet()) { if (index == target) { return it.getValue(); } index++; }
I have to get some random elements indexed about 50 times from the map There will be about 20 items on the map
thank you
Solution
List<Entry<String,Foo>> randAccess = new ArrayList<Entry<String,Foo>>(foos.entrySet());
List<Entry<String,Foo>> randAccess = new ArrayList<Entry<String,Foo>>(foos.entrySet());
Then for index n and O (1) access
randAccess.get(N)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码