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 map m if m.put(k, v) invoked when m.containskey(k) return true prior invocation.)

this implementation spares clients unspecified, chaotic ordering provided hashmap (and hashtable), without incurring increased cost associated treemap.

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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -