我有一个LinkedHashMap.我想在索引N中获得Foo.除了迭代之外,还有更好的方法来做这个事情吗?
- int target = N;
- int index = 0;
- for (Map.Entry<String,Foo> it : foos.entrySet()) {
- if (index == target) {
- return it.getValue();
- }
- index++;
- }
我必须从地图获取一些索引约50次的随机元素.地图将有约20项.
谢谢
解决方法
- List<Entry<String,Foo>> randAccess = new ArrayList<Entry<String,Foo>>(foos.entrySet());
然后对于索引N与O(1)访问…
- randAccess.get(N)