How to sort Java hashtable?

I inserted some data into a Java Hashtable. If I read the data from the Hashtable, it will not return in the same order that I inserted it. How to get ordered data from a hashtable?

I use the following code to get values ​​from a hash table:

// 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() ); } 
+7
java sorting hashtable maps
source share
7 answers

If you need a map that preserves order, you should use LinkedHashMap :

A hash table and associated map interface list with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly linked list passing through all its entries. This linked list defines the iteration order, which is usually the order in which keys were inserted into the map (insert order). Please note that the insertion order does not change if the key is reinserted into the card. (The key k reinserted into map m if m.put(k, v) is called when m.containsKey(k) returns true just before the call.)

This implementation relieves customers of the unspecified, usually chaotic order provided by the HashMap (and Hashtable ), without increasing the cost associated with TreeMap .

Note that this is usually compared to a HashMap , not a Hashtable — I don’t know the equivalent of keeping the Hashtable order; the latter is not commonly used these days (just like an ArrayList commonly used in the Vector preference).

I suggested that you need an insertion order, not a sort order by keys. If you want to use the latter, use TreeMap .

+20
source share

A Hashtable has no predictable iteration order and cannot be sorted. If you only need a predictable iteration order, you should use LinkedHashMap . If you want to sort your Map , you must use TreeMap .

+7
source share

Although the Hashtable cannot be sorted, he asked how to get the sorted data that can be done to sort the list of keys extracted from the Hashtable and get the 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 good.

+6
source share

Hashtable is an obsolete collection that was replaced by Java 1.2 collections in 1998. I suggest you avoid this, along with Vector and Enumeration .

Instead of a Hashtable use a HashMap where possible. You can add synchronization using Collections.synchronizedMap(map) if you need it.

Instead of Vector use an ArrayList where possible. You can add synchronization using Collections.synchronizedList(map) if you need it.

Instead of Enumeration you can use an Iterator loop or even for-each .

+3
source share

Use TreeMap to sort 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); 
+2
source share

If I read the data from the hash table it does not come in the same order that I inserted.

Your question does not make sense. A hashtable does not have an “order”, it is unordered (Edit: some implementations have an order, but this is not common for a hash table).

In what order do you expect the entries to be?

If you want to store items in a specific order, you need to use a list (e.g. a subclass of java.util.List).

And BTW, your sample code doesn't even have a hash table.

0
source share

I wrote a detailed answer about sorting and ordered extraction of Maps in this previous Question:

Access to the latest map entry

0
source share

All Articles