How to keep the order of elements in hashtable
Asked Answered
A

5

26

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.

Aery answered 14/9, 2009 at 4:42 Comment(2)
Which language are you using?Huckaby
LinkedHashMap is a part of Java.Danitadaniyal
L
37

Use a LinkedHashMap.

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in 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-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.)

combined with Collections.synchronizedMap().

So, for example:

Map<String, String> map = Collections.synchronizedMap(
  new LinkedHashMap<String, String>());
Lichee answered 14/9, 2009 at 4:47 Comment(2)
To clarify : If I'm not looking at having more then 1 tread access this LinkedHashMap at a time, would it still be recommended to synchronize it? Or the LinkedHashMap on it's own would be considered enough?Gerik
@Gerik The LinedHashMap should be enough on its own.Retrospection
L
4

You could either wrap a LinkedHashMap and synchronize or you could use the Collections.synchronizedMap utility to create a synchronized LinkedHashMap:

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

From the 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

Liebowitz answered 14/9, 2009 at 4:49 Comment(1)
The performance overhead will be negligable (literally nanoseconds).Creswell
S
3

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).

Selfsupporting answered 14/9, 2009 at 4:47 Comment(0)
H
2

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.

Higher answered 14/9, 2009 at 4:46 Comment(0)
F
1

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

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

if you want sorted then use ConcurrentSkipListMap

Forestry answered 23/10, 2009 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.