本文介绍了以相反的顺序遍历 LinkedHashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 LinkedHashMap:
I have a LinkedHashMap:
LinkedHashMap<String, RecordItemElement>
我需要从给定键的位置向后迭代.因此,如果给我第 10 个项目的键,我需要向后遍历哈希图 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!