具有键/值对的java集合,并且根据插入顺序排序 [英] java collection that has key/value pair, and is ordered according to insert order
问题描述
我想要的东西,我可以插入键/值对,顺序是我插入的项目的顺序。
I want something where I can insert key/value pairs, and the order is the order that I insert the items in.
我看到一些关于地图的,但似乎我必须为他们写我自己的比较器。
I have seen some posts regarding map's, but it seems I have to write my own comparator for them.
我想要插入的第一个项目是第一个存储,第二个是第二个项目
I want the first item I insert to be the first stored, and the 2nd be the 2nd item in the collection ,etc.
推荐答案
尝试使用 LinkedHashMap
javadocs :
Map接口的哈希表和链表实现,具有可预测的迭代顺序这个实现不同于HashMap,因为它维护一个贯穿其所有条目的双向链表。此链接列表定义了迭代排序,它通常是将键插入到地图中的顺序(插入顺序)。
Hash table and linked list implementation of the Map interface, with predictable iteration order This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
这篇关于具有键/值对的java集合,并且根据插入顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!