Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sorting hashtable, need explaination

Posted on 2004-10-09
11
Medium Priority
?
237 Views
Last Modified: 2010-03-31
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
Comment
Question by:polkadot
[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
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 

Author Comment

by:polkadot
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

by:sudhakar_koundinya
ID: 12267027
Can you explain why you need to sort Hashtable()??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12267043
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 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

by:sciuriware
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

by:sudhakar_koundinya
ID: 12267068
0
 

Author Comment

by:polkadot
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

by:polkadot
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

by:sciuriware
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

by:sudhakar_koundinya
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

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question