Returns an alphabetically sorted list of the keys. - Java java.util

Java examples for java.util:List Creation

Description

Returns an alphabetically sorted list of the keys.

Demo Code


//package com.java2s;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Vector;

public class Main {
    /** Returns an alphabetically sorted list of the keys. */
    public static Vector getSortedKeyList(Hashtable hashtable) {
        Vector result = new Vector();
        Enumeration keys = hashtable.keys();
        while (keys.hasMoreElements()) {
            result.add(keys.nextElement());
        }/*  w  w  w  .jav a 2 s .c  o m*/
        Collections.sort(result, new Comparator() {
            public int compare(Object a, Object b) {
                String textA = a.toString();
                String textB = b.toString();

                return textA.compareToIgnoreCase(textB);
            }
        });

        return result;
    }
}

Related Tutorials