简体   繁体   English

使用插入顺序作为顺序实现java.util.Map

[英]Implementation of java.util.Map using insertion order as order

The JDK documentation on java.util.Map states 有关java.util.Map的JDK文档说明

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. 地图的顺序定义为地图集合视图上的迭代器返回其元素的顺序。 Some map implementations, like the TreeMap class, make specific guarantees as to their order; 一些地图实现,比如TreeMap类,对它们的顺序做出了特定的保证; others, like the HashMap class, do not. 其他人,比如HashMap类,没有。

And the documentation on TreeMap states: 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. 地图根据其键的自然顺序进行排序,或者根据使用的构造函数在地图创建时提供的比较器进行排序。

Is there any JDK implementation of Map that uses the order that elements were inserted into the map as the order? 是否有任何Map的JDK实现使用元素作为顺序插入到地图中的顺序?

java.util.LinkedHashMap : java.util.LinkedHashMap

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. 此实现与HashMap不同之处在于它维护了一个贯穿其所有条目的双向链表。 This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map ( insertion-order ). 此链接列表定义迭代排序,通常是键插入映射的顺序插入顺序 )。 Note that insertion order is not affected if a key is re-inserted into the map. 请注意,如果将键重新插入地图,则插入顺序不会受到影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM