How to sort a Java Hashtable?
Solution 1
If you want an order-preserving map, you should use 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 mapm
ifm.put(k, v)
is invoked whenm.containsKey(k)
would return true immediately prior to the invocation.)This implementation spares its clients from the unspecified, generally chaotic ordering provided by
HashMap
(andHashtable
), without incurring the increased cost associated withTreeMap
.
Note that this is usually compared with HashMap
rather than Hashtable
- I don't know of an order-preserving equivalent to Hashtable
; the latter isn't usually used these days anyway (just as ArrayList
is usually used in preference to Vector
).
I've assumed you want insertion order rather than key-sorted order. If you want the latter, use TreeMap
.
Solution 2
A Hashtable
has no predictable iteration order, and cannot be sorted. If you only want predictable iteration order you should use a LinkedHashMap
. If you want to be able to sort your Map
, you should use a TreeMap
.
Solution 3
Although a Hashtable
cannot be sorted, he asked how to get sorted data, that can be done sorting the list of keys extracted from the HashTable
and retrieving values in that order.
Something like:
List<'your_type'> tmp = Collections.list('your_hashtable'.keys());
Collections.sort(tmp);
Iterator<'your_type'> it = tmp.iterator();
while(it.hasNext()){
'your_type' element =it.next();
//here you can get ordered things: 'your_hashtable'.get(element);
}
will be fine.
Solution 4
Hashtable
is a legacy collection which was replaced by Java 1.2 collections in 1998. I suggest you avoid it, along with Vector
and Enumeration
.
Instead of Hashtable
use HashMap
where possible. You can add synchronization using Collections.synchronizedMap(map)
if you need it.
Instead of Vector
, use ArrayList
where possible. You can add synchronization using Collections.synchronizedList(map)
if you need it.
Instead of Enumeration
you can use Iterator
or even a for-each
loop.
Solution 5
Use TreeMap for sorting it:
Map<String, String> yourMap = new HashMap<String, String>();
yourMap.put("1", "one");
yourMap.put("2", "two");
yourMap.put("3", "three");
Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);
Tester
Updated on March 19, 2020Comments
-
Tester about 4 years
I inserted some data into a Java Hashtable. If I read the data from the Hashtable it doesn't come back in the same order that I inserted it in. How do I get the ordered data from the Hashtable?
I use the following code to get the values from the hashtable:
// Get a set of the entries Set set = hsUpdateValues.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); System.out.print( "Key : " + me.getKey() + ", Value: " + me.getValue() ); }
-
Jon Skeet over 13 yearsThat won't give insertion order though.
-
GreyCat over 13 years@Tester: you'd likely want to "accept" an answer, if it's okay for you :)
-
Grumblesaurus about 9 yearsWhat would you use instead?
-
Thiam Hooi almost 3 yearsExample can be found at link