简体   繁体   English

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

[英]How to keep the order of elements in hashtable

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是一种替代方法,但它不是同步的。

Use a LinkedHashMap . 使用LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. Map接口的哈希表和链表实现,具有可预测的迭代顺序。 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. 请注意,如果将键重新插入地图,则插入顺序不会受到影响。 (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.) (如果密钥K被重新插入到地图米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>());

You could either wrap a LinkedHashMap and synchronize or you could use the Collections.synchronizedMap utility to create a synchronized LinkedHashMap : 您可以包装LinkedHashMap并进行同步,也可以使用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. 如果不存在此类对象,则应使用Collections.synchronizedMap方法“包装”该映射。 This is best done at creation time, to prevent accidental unsynchronized access to the map 这最好在创建时完成,以防止意外地不同步访问地图

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). 因此,我建议使用外部结构来维护排序,并使用哈希表来存储值(用于快速查找)。

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. 由于您没有指定您正在使用的语言,因此我无法建议替代语言,但您需要某种类型的有序键/值集。

If jdk1.6 you have only two type of ordered map EnumMap and LinkedHashMap. 如果jdk1.6你只有两种类型的有序映射EnumMap和LinkedHashMap。 Both of them are not synchronized. 它们都不同步。 If you just need to remember the order, use 如果您只需要记住订单,请使用

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

if you want sorted then use ConcurrentSkipListMap 如果你想要排序然后使用ConcurrentSkipListMap

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

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