[英]Why don't TreeMap.values() reflect the order in which elements were originally added?
I need a data structure
that will perform both the role of a lookup map
by key as well as be able to be convertable into a sorted list
. 我需要一个
data structure
,该data structure
既可以通过键执行lookup map
的角色,又可以转换为sorted list
。 The data that goes in is a very siple code-description pair (eg M/Married , D/Divorced etc). 输入的数据是非常简单的代码描述对(例如, M /已婚 , D /离婚等)。 The lookup requirement is in order to get the description once the user makes a selection in the UI, whose value is the code.
一旦用户在UI中进行选择(其值为代码),则查找要求就是为了获得描述。 The sorted list requirement is in order to feed the data into UI components (JSF) which take
List
as input and the values always need to be displayed in the same order (alphabetical order of description). 排序列表要求是为了将数据馈送到以
List
为输入的UI组件(JSF)中,并且始终需要以相同的顺序(描述的字母顺序)显示值。
The first thing that came to mind was a TreeMap . 首先想到的是TreeMap 。 So I retrieve the data from my DB in the order I want it to be shown in the UI and load it into my tree map, keyed by the code so that I can later look up descriptions for further display once the user makes selections.
因此,我按希望在UI中显示的顺序从数据库中检索数据,然后将其加载到树形地图中,并按代码进行键控,以便以后在用户进行选择后可以查找说明以进一步显示。 As for getting a sorted list out of that same map, as per this post , I am doing the following:
至于从同一张地图中获取排序列表,按照这篇文章 ,我正在做以下事情:
List<CodeObject> list = new ArrayList<CodeObject>(map.values());
However, the list is not sorted in the same order that they were put into the map. 但是,列表的排列顺序与放入地图的顺序不同。 The map is declared as a SortedMap and implemented as a TreeMap:
该地图被声明为SortedMap并被实现为TreeMap:
SortedMap<String, CodeObject> map = new TreeMap<String, CodeObject>().
CodeObject
is a simple POJO
containing just the code and description and corresponding getters (setters in through the constructor), a list of which is fed to UI components, which use the code as the value and description for display. CodeObject
是一个简单的POJO
,仅包含代码和描述以及相应的getter(通过构造函数输入),其列表被馈送到UI组件,UI组件使用代码作为显示的值和描述。 I used to use just a List and that work fine with respect to ordering but a List does not provide an efficient interface for looking up a value by key and I now do have that requirement. 我曾经只使用一个List,并且可以很好地进行订购,但是List并没有提供用于按键查找值的有效接口,现在我确实有该要求。
So, my questions are: 因此,我的问题是:
TreeMap.values()
in the same order? TreeMap.values()
的顺序相同? TreeMap
maintain's the key's natural order. TreeMap
维护密钥的自然顺序。 You can even order it (with a bit more manipulation and custom definition of a comparator) by the natural order/reverse order of the value. 您甚至可以按值的自然顺序/反向顺序对其进行排序(需要更多的操作和对比较器的自定义)。 But this is not the same as saying "Insertion order".
但这与说“插入顺序”不同。 To maintain the insertion order you need to use
LinkedHashMap
. 要维持插入顺序,您需要使用
LinkedHashMap
。 Java LinkedHashMap
is a subclass of HashMap
- the analogy is the same as LinkedList
where you maintain the trace of the next node. Java
LinkedHashMap
是HashMap
的子类-类推与LinkedList
相同,您可以在其中维护下一个节点的跟踪。 However, it says it cannot "Guarantee" that the order is maintained, so don't ask your money back if you suddenly see an insertion order is maintained with HashMap
但是,它说不能“保证”订单的维护,因此,如果突然发现
HashMap
维护了插入订单,请不要退钱。
TreeMap
's documentation says: TreeMap
的文档说:
The map is sorted according to the natural ordering of its keys, or by a
Comparator
provided at map creation time, depending on which constructor is used.根据映射键的自然顺序或在映射创建时提供的
Comparator
对映射进行排序,具体取决于所使用的构造函数。
So unless you're providing a Comparator
and tracking the insertion order and using it in that Comparator
, you'll get the natural order of the keys, not the order in which the keys were inserted. 因此,除非您提供一个
Comparator
并跟踪插入顺序并在该Comparator
使用它,否则您将获得键的自然顺序,而不是键的插入顺序。
If you want insertion order, as davide said , you can use LinkedHashMap
: 如果您想要插入顺序( 如davide所说) ,则可以使用
LinkedHashMap
:
Hash table and linked list implementation of the
Map
interface, with predictable iteration order...This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map ( insertion-order ).Map
接口的哈希表和链表实现,具有可预测的迭代顺序...此链表定义了迭代顺序,通常是将键插入到映射中的顺序 ( 插入顺序 )。 Note that insertion order is not affected if a key is re-inserted into the map.请注意,如果将密钥重新插入到映射中,则插入顺序不会受到影响。
您需要的是LinkedHashMap以及另一个问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.