Java 如何保持哈希表中元素的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1419708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:53:25  来源:igfitidea点击:

How to keep the order of elements in hashtable

javahashtablelinkedhashmap

提问by Thomas Manalil

I have a hashtable . values() method returns values in some order different from the order in which i am inserted.How can i get the values in the same order as i inserted?Using LinkedHashmap is an alternative but it is not synchronized.

我有一个哈希表。values() 方法以与插入顺序不同的顺序返回值。如何以与插入的顺序相同的顺序获取值?使用 LinkedHashmap 是一种替代方法,但它不同步。

采纳答案by cletus

Use a LinkedHashMap.

使用一个LinkedHashMap.

Hash table and linked list implementation of the Mapinterface, with predictable iteration order. This implementation differs from HashMapin 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). Note that insertion order is not affected if a key is re-insertedinto the map. (A key k is reinserted into a map m if m.put(k, v)is invoked when m.containsKey(k)would return trueimmediately prior to the invocation.)

接口的哈希表和链表实现Map,具有可预测的迭代顺序。此实现的不同之处HashMap在于它维护一个贯穿其所有条目的双向链表。这个链表定义了迭代顺序,通常是将键插入到映射中的顺序插入顺序)。请注意,如果将键重新插入映射中,则插入顺序不会受到影响。(键 k 被重新插入映射 m 如果 m.put(k, v)在调用 之前立即m.containsKey(k)返回时 true被调用。)

combined with Collections.synchronizedMap().

结合Collections.synchronizedMap().

So, for example:

因此,例如:

Map<String, String> map = Collections.synchronizedMap(
  new LinkedHashMap<String, String>());

回答by Ed S.

A hash table is inherently unordered, so you are using the wrong data structure. Since you don't specify what language you are using I cannot suggest an alternate, but you need some type of ordered key/value set.

哈希表本质上是无序的,因此您使用了错误的数据结构。由于您没有指定您使用的语言,我不能建议替代,但您需要某种类型的有序键/值集。

回答by Nippysaurus

I'm pretty sure that the reason hashtables are unsorted is to aid storage and retrieval speed. Because of this I would suggest using an external structure to maintain ordering and just using the hashtable for storing values (for fast lookup).

我很确定哈希表未排序的原因是为了提高存储和检索速度。因此,我建议使用外部结构来维护排序并仅使用哈希表来存储值(用于快速查找)。

回答by akf

You could either wrap a LinkedHashMapand synchronize or you could use the Collections.synchronizedMaputility to create a synchronized LinkedHashMap:

您可以包装 aLinkedHashMap并进行同步,也可以使用该Collections.synchronizedMap实用程序创建同步LinkedHashMap

Map m = Collections.synchronizedMap(new LinkedHashMap(...));

From the JavaDoc:

来自 JavaDoc:

If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map

如果多个线程同时访问链接的哈希映射,并且至少有一个线程在结构上修改映射,则必须在外部进行同步。这通常是通过同步一些自然封装地图的对象来完成的。如果不存在这样的对象,则应使用 Collections.synchronizedMap 方法“包装”地图。这最好在创建时完成,以防止对地图的意外不同步访问

回答by DKSRathore

If jdk1.6 you have only two type of ordered map EnumMap and LinkedHashMap. Both of them are not synchronized. If you just need to remember the order, use

如果 jdk1.6 你只有两种类型的有序映射 EnumMap 和 LinkedHashMap。两者不同步。如果您只需要记住顺序,请使用

Map m = Collections.synchronizedMap(new LinkedHashMap(...));

if you want sorted then use ConcurrentSkipListMap

如果你想排序然后使用 ConcurrentSkipListMap