Skip to content

Categories:

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;
}

No hay artículos relacionados. Echa un ojo a estos:

    Posted in Sin categoría.


    One Response

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

    1. Ignacio Sánchez says

      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

    or, reply to this post via trackback.