以相反的顺序遍历 LinkedHashMap

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)