以相反的顺序遍历 LinkedHashMap [英] Iterating through a LinkedHashMap in reverse order

查看:40
本文介绍了以相反的顺序遍历 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆