以相反顺序遍历LinkedHashMap [英] Iterating through a LinkedHashMap in reverse order
本文介绍了以相反顺序遍历LinkedHashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个LinkedHashMap:
I have a LinkedHashMap:
LinkedHashMap<String, RecordItemElement>
,我需要从给定键的位置向后迭代。因此,如果我获得了第10项的关键字,则需要通过hashmap 9,8,7等向后迭代。
that I need to iterate through from a given key's position, backwards. So if I was given the 10th item's key, I'd need iterate backwards through the hashmap 9, 8, 7 etc.
推荐答案
你不必遍历它。但是,将键关闭并将其存储在列表中将会很方便。这是您可以执行indexOf()类型操作的唯一方法。
You don't have to iterate through it. But it would be handy to pull the keys off and store it in a list. Thats the only way you can do indexOf() type operations.
List<String> keyList = new ArrayList<String>(map.keySet());
// Given 10th element's key
String key = "aKey";
int idx = keyList.indexOf(key);
for ( int i = idx ; i >= 0 ; i-- )
System.out.println(map.get(keyList.get(i)));
这篇关于以相反顺序遍历LinkedHashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文