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()
);
}
The Hashtable mappings can be sorted using the following two ways: Using TreeMap. Using LinkedHashMap.
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.
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.
HashTable implements a dictionary, and total order of insertions is not retained because insertions with different hash values map to different chains.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With