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.
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;
}
One Response
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
Muy buen aporte Coler.
Sólo una preguntilla. El tipo de algoritmo que has puesto, ¿es un burbuja, quicksort, mergesort, …. cosecha propia?
Un saludo