简体   繁体   English

为什么TreeMap.values()不能反映最初添加元素的顺序?

[英]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: 因此,我的问题是:

  1. If TreeMap is supposed to be a map in the ordered of item addition, why isn's TreeMap.values() in the same order? 如果TreeMap应该是按项目添加顺序排列的地图,为什么TreeMap.values()的顺序相同?
  2. What should I do to fulfill my requirements explained above, ie have a data structure that will serve as both a lookup map AND a sorted collection of elements? 我应该怎么做才能满足我上面解释的要求,即拥有既可以用作查找图又可以用作元素排序集合的数据结构? Will TreeMap do it for me if I use it differently or do I need an altogether different approach? 如果我以不同的方式使用TreeMap,或者我需要完全不同的方法,TreeMap会为我做到吗?

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 LinkedHashMapHashMap的子类-类推与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.

相关问题 为什么像 HashMap 或 LinkedList 这样的 Java 容器不重用在添加新元素时被删除的内部节点? - Why don`t Java containers like HashMap or LinkedList reuse internal Nodes which were deleted when adding new elements? 组 <String> 顺序与元素添加顺序不同 - Set<String> doesn't maintain order the same as the order the elements were added in JPanel GridLayout(由循环部分填充)将新值添加到结尾,而不考虑添加顺序 - JPanel GridLayout (filled partially by loop) adds new values to end regardless of order in which they were added HashMap不按添加顺序提供值 - HashMap doesn't provide values in the order in which added them 按字母顺序对 TreeMap 值进行排序 - Sort TreeMap Values In Alphabetical order 如何使用Java在TreeMap中放置元素的顺序? - How would I access the order in which I placed elements in a TreeMap in Java? GridBagLayout不排序我的元素 - GridBagLayout don't order my elements Java Multimap的自然排序键,但集合按添加元素的顺序排序 - Java Multimap of naturally ordered keys but collections sorted in the order the elements were added 比较两个迭代器,并检查两个元素之间添加,删除或相同的元素 - Comparing two iterators and checking which elements were added, removed or the same between the two RxJava,为什么1和2没有按这个顺序依次到达? - RxJava, why 1 and 2 don't arrive in order in this sequence?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM