Monster Monster - 1 year ago 65
Java Question

Is there a simple way to alphabetize a String Enumeration in Java?

Just as the title says.

I tried messing around a bit with Collections.sort() on a List[] and the .sort() function of an ArrayList but I was never able to parse it back to an Enumeration.



Here's some pseduocode and further explanation. My goal is to take the keys() from a Hashtable and do complex operations involving each one, alphabetically.

My current process is:

  1. Take a hash table I'm given

  2. Form an enumeration from the ht

  3. Run a while loop until the enumeration is empty

So the code is like this:

public void processData(Hashtable<String,GenericClass> htData)
Enumeration<String> enData = htData.keys();

String current = enData.nextElement();


The problem, is that the data in the enumeration has to be alphabetical (that is, the "complex process" must be done on each key in alphabetical order). Any solutions? Thanks!

Answer Source

If you have a sorted list you can use


to get it back to an enumeration... if I'm following you correctly..


You can do this...

List l = new ArrayList(ht.keySet());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download