Iterating through a LinkedHashMap in reverse order(以相反的顺序遍历 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以相反的顺序遍历 LinkedHashMap


基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01