Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a Java Hashtable?

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()
    );
}
like image 211
Tester Avatar asked Dec 02 '10 09:12

Tester


People also ask

Can we sort HashTable in Java?

The Hashtable mappings can be sorted using the following two ways: Using TreeMap. Using LinkedHashMap.

How do I sort a HashTable?

You have a hashtable of keys and values, and want to get the list of values that result from sorting the keys in order. To sort a hashtable, use the GetEnumerator() method on the hashtable to gain access to its individual elements. Then use the SortObject cmdlet to sort by Name or Value.

Can we sort a HashTable?

Before moving forward, we need to understand that it is not possible to sort a Hashtable since the data is stored by the hashcode of the key, not by the index . So to sort the data of a Hashtable, we need to have a sortable object like an array or an ArrayList. Sorting has to be done on Key or Value.

Why is HashTable not ordered?

HashTable implements a dictionary, and total order of insertions is not retained because insertions with different hash values map to different chains.


2 Answers

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 map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable), without incurring the increased cost associated with TreeMap.

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.

like image 168
Jon Skeet Avatar answered Oct 24 '22 23:10

Jon Skeet


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.

like image 45
Filippo Mazza Avatar answered Oct 25 '22 01:10

Filippo Mazza