2

my problem is i have to parsed these values

Cijfercode 
Crypto 
Doorlopr 
Kruizword 
Woordzoker
Zweeds

from an xml file.but in xml they are in the alphabetical order same as above.but while using hashtable to retrive values (these values are not keys of this hashtable)it becomes the order

Cijfercode
Doorloper
Woordzoker
Zweeds
Kruizword
Crypto 

why is is happening?

2 Answers 2

7

You need to use LinkedHashMap to maintain the order in which the elements where inserted, (or a TreeMap if you need some custom ordering, such as alphabetical, regardless of insertion order).

A HashTable does not preserve any order. Just like Set it's an unordered data structure.

Sign up to request clarification or add additional context in comments.

7 Comments

ok but firsly values are added alphabetical order in the hashtable.
yep. Unfortunately that doesn't matter. It should work as you expect if you use a LinkedHashMap.
but retriving from hashtable order changes.
so ? If you are looking at retrieving elements in the same order it was inserted , you shouldnt be using an unorderded data structure , should you ?
LinkedHashMap maintains the order of the elements same as they we inserted; if that is what you want then use LinkedHashMap. If you want to get natural ordering of elements, say alphabetically, then TreeMap should be used.
|
0

HashTable and HashMap both do not guarantee the ordering of it's elements. You should use TreeMap for such purposes.

1 Comment

Hi every one. i got a solution for sorting the hashtable elements .Here is the solution java.util.Vector vec =new java.util.Vector(hashtableList.keySet()); Collections.sort(vec);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.