Solved

# sorting hashtable, need explaination

Posted on 2004-10-09
236 Views
I have seen a bunch of code to do this, but I am really confused on why things are implemented. Could someone give me the right way to sort hashtable by keys, if my key is a double and my value is  string... and provide explanation.

Is there anything like for Hashtable h;
h.sortbyKey();
???
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 3

Author Comment

ID: 12267003
or how to enumerate the hash table, by key. I understand the concept of enumeration, just not the code in java. Thanks.
0

LVL 14

Expert Comment

ID: 12267027
Can you explain why you need to sort Hashtable()??
0

LVL 14

Expert Comment

ID: 12267043
0

LVL 14

Accepted Solution

sudhakar_koundinya earned 500 total points
ID: 12267046
import java.util.Hashtable;
import java.util.Vector;
import java.util.Collections;
import java.util.Enumeration;

public class SortHashtable {

public static void main(String[] args) {
// Create and populate hashtable
Hashtable ht = new Hashtable();
ht.put("ABC", "abc");
ht.put("XYZ", "xyz");
ht.put("MNO", "mno");

// Sort hashtable.
Vector v = new Vector(ht.keySet());
Collections.sort(v);

// Display (sorted) hashtable.
for (Enumeration e = v.elements(); e.hasMoreElements();) {
String key = (String)e.nextElement();
String val = (String)ht.get(key);
System.out.println("Key: " + key + "     Val: " + val);
}
}
}
0

LVL 24

Expert Comment

ID: 12267051
You can enumerate a Hashtable by means of an Iterator (to travel along all stored keys).
If you want to sort the contents you better transfer the contents to a TreeSet.
;JOOP!
0

LVL 14

Expert Comment

ID: 12267068
0

Author Comment

ID: 12267070
I guess I'm not really sorting, I just want to be able to retrieve the items of the hashtable in the numerical order of the keys
0

Author Comment

ID: 12267098
yeah I think your first answer is what I want ... was hoping for a bit more explanation of the code, but its ok.
0

LVL 24

Expert Comment

ID: 12267102
Well a HashTable is optimised for retrieval speed, so it isn't sorted.
And thus you must sort the contents in order to do what you want.
;JOOP!
0

LVL 14

Expert Comment

ID: 12267157
>>was hoping for a bit more explanation of the code, but its ok.

1.First that code gets the keys as Set
2. Converts the Set to Vector.
3. Sort the Vector using Collections.sort(List)
4. Traverse the Hashtable using Sorted keys

0

LVL 24

Expert Comment

ID: 12267176
As you can see I work on a very slow line.
;JOOP!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how â€¦
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a vâ€¦
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
###### Suggested Courses
Course of the Month8 days, 3 hours left to enroll