sorting - How to sort a Java Hashtable? -
i inserted data java hashtable. if read data hashtable doesn't come in same order inserted in. how ordered data hashtable?
i use following code values hashtable:
// set of entries set set = hsupdatevalues.entryset(); // iterator iterator = set.iterator(); // display elements while (i.hasnext()) { map.entry me = (map.entry) i.next(); system.out.print( "key : " + me.getkey() + ", value: " + me.getvalue() ); }
if want order-preserving map, should use linkedhashmap
:
hash table , linked list implementation of map interface, predictable iteration order. implementation differs hashmap in maintains doubly-linked list running through of entries. linked list defines iteration ordering, order in keys inserted map (insertion-order). note insertion order not affected if key re-inserted map. (a key
k
reinserted mapm
ifm.put(k, v)
invoked whenm.containskey(k)
return true prior invocation.)this implementation spares clients unspecified, chaotic ordering provided
hashmap
(andhashtable
), without incurring increased cost associatedtreemap
.
note compared hashmap
rather hashtable
- don't know of order-preserving equivalent hashtable
; latter isn't used these days anyway (just arraylist
used in preference vector
).
i've assumed want insertion order rather key-sorted order. if want latter, use treemap
.
Comments
Post a Comment