Share this page 

Sort an HashtableTag(s): Language


Data in hashtable are not sorted. We extract the keys and sort them.
import java.util.*;

public class HashtableTest {

  static String[] array1 = {"C","B","A"};
  static String[] array2 = {"1","2","3"};

  public static void main(String args[]) {

    Hashtable h = new Hashtable();
    h.put(array1[0], array2[0]);
    h.put(array1[1], array2[1]);
    h.put(array1[2], array2[2]);

    // unsorted keys output
    Iterator it = h.keySet().iterator();
    while (it.hasNext()) {
       String element =  (String)it.next();
       System.out.println(element + " " + (String)h.get(element));
    }

    System.out.println("============");

    // sorted keys output  thanks to T. GUIRADO for the tip!
    Vector v = new Vector(h.keySet());
    Collections.sort(v);
    it = v.iterator();
    while (it.hasNext()) {
       String element =  (String)it.next();
       System.out.println( element + " " + (String)h.get(element));
    }
    /*
    output :
        A 3
        C 1
        B 2
        ============
        A 3
        B 2
        C 1
    */
  }
}
NOTE : When possible always use an HashMap instead of an Hashtable. Since Hashtable methods are synchronized they are slower than those in HashMap.

See this Howto to sort an HashMap.

See this Howto to sort a Properties.