Skip to content


Ordenar Hashtable en Java

Nos puede resultar bastante útil obtener de forma ordenada alfabéticamente los datos que hemos introducido en una Hashtable, para ello este método al que le pasamos el objeto Hashtable, nos devolverá un array de Strings con las keys ordenadas.

private String[] keysOrdenadas(Hashtable ht) {
Object[] valores = ht.keySet().toArray();
ArrayList lista = new ArrayList();

for (int i = 0; i < valores.length; i++) {
lista.add((String) valores[i]);
}
Collections.sort(lista);

Object[] valores_ordenados = lista.toArray();

String[] indice = new String[valores_ordenados.length];
for (int i = 0; i < valores_ordenados.length; i++) {
indice[i] = (String) valores_ordenados[i];
}

return indice;
}

Artículos Relacionados

Posted in Java. Tagged with , , .

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Muy buen aporte Coler.

    Sólo una preguntilla. El tipo de algoritmo que has puesto, ¿es un burbuja, quicksort, mergesort, …. cosecha propia?

    Un saludo

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.