Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

sorting hashtable, need explaination

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
polkadot
Asked:
polkadot
  • 5
  • 3
  • 3
1 Solution
 
polkadotAuthor Commented:
or how to enumerate the hash table, by key. I understand the concept of enumeration, just not the code in java. Thanks.
0
 
sudhakar_koundinyaCommented:
Can you explain why you need to sort Hashtable()??
0
 
sudhakar_koundinyaCommented:
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sudhakar_koundinyaCommented:
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
 
sciuriwareCommented:
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
 
sudhakar_koundinyaCommented:
0
 
polkadotAuthor Commented:
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
 
polkadotAuthor Commented:
yeah I think your first answer is what I want ... was hoping for a bit more explanation of the code, but its ok.
0
 
sciuriwareCommented:
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
 
sudhakar_koundinyaCommented:
>>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
 
sciuriwareCommented:
As you can see I work on a very slow line.
;JOOP!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now