以相反顺序迭代 LinkedHashMap
我有一个 LinkedHashMap:
LinkedHashMap<String, RecordItemElement>
我需要从给定键的位置向后迭代。因此,如果给我第 10 个项目的键,我需要向后迭代哈希图 9、8、7 等。
I have a LinkedHashMap:
LinkedHashMap<String, RecordItemElement>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这个问题需要一个相反顺序的 LinkedHashMap,一些答案建议使用 TreeSet,但这会根据键重新排序映射。
该解决方案允许对原始 LinkedHashMap 进行迭代,而不是像已经提议的那样对新的 ArrayList 进行迭代:
The question requires a LinkedHashMap in reverse order, some answers suggesting using a TreeSet but this will reorder the map based upon the key.
This solution allows the iteration over the original LinkedHashMap not the new ArrayList as has also been proposed:
HashMap:
反向迭代值:
反向迭代键:
反向迭代两者:
The HashMap:
Reverse iterating over values:
Reverse iterating over keys:
Reverse iterating over both:
您不必迭代它。但拔下钥匙并将其存储在列表中会很方便。这是执行 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.
从 Java 21 开始,LinkedHashMap 具有
reversed()
方法,返回地图的反转视图。这可以以标准方式迭代。要根据请求专门从给定键开始,可以使用反向映射条目集上的
Stream
,使用dropWhile
跳过所有元素的方法在该键之前遇到:As of Java 21,
LinkedHashMap
has thereversed()
method, which returns a reversed view of the map. This can be iterated over in the standard manner.To specifically start with a given key as requested, a
Stream
over the reversed map's entry set could be used, using thedropWhile
method to skip all elements encountered prior to that key:使用“user22745008”解决方案和labdas以及一些泛型,您可以得到一个非常简洁的解决方案作为方法:
Using "user22745008" solution and labdas with some generics you can have a very neat solution as a method:
这是一个老问题,但我认为它缺乏采用更新方法的答案。以下使用 Java 9 功能:
上面的代码流式传输映射的条目集,保留条目,直到找到与给定键相等的键。然后,条目被收集到 ArrayDeque 中。
不过,有一个细节缺失。根据您是否需要将与给定键匹配的条目也包含在结果中,您可能需要手动将其添加到双端队列中。如果您不想添加它,那么就完成了。否则,只需执行以下操作:
现在,要以相反的顺序迭代
Deque
,只需使用其descendingIterator()
:值得一提的是,此方法仅在流是顺序。无论如何,我们在这里使用并行流不会获得任何东西。
This is an old question, but I think it's lacking an answer that takes a newer approach. The following uses Java 9 features:
The code above streams the map's entryset, keeping entries until a key equal to the given key is found. Then, the entries are collected to an
ArrayDeque
.One detail is missing, though. Depending on whether you need the entry that matches the given key to also be included in the result or not, you might need to manually add it to the deque. If you don't want it added, then you're done. Otherwise, simply do:
Now, to iterate the
Deque
in reverse order, simply use itsdescendingIterator()
:It's worth mentioning that this approach only works if the stream is sequential. Anyways, we wouldn't have gained anything using a parallel stream here.