import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
//public static void ordenamientoArreglos(String[] palabras, int[] apariciones) {
public static LinkedHashMap<Character, Integer> ordenar(Map<Character, Integer> m) {
char[] caracter = new char[m.size()];
int[] apariciones = new int[m.size()];
int a = 0;
for (char c : m.keySet()) {
caracter[a] = c;
apariciones[a] = m.get(c);
a++;
}
//Aquí ordenamos por burbuja
for (int i = caracter.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (j + 1 <= i && apariciones[j] < apariciones[j + 1]) {
char aux = caracter[j];
int iux = apariciones[j];
caracter[j] = caracter[j + 1];
apariciones[j] = apariciones[j + 1];
caracter[j + 1] = aux;
apariciones[j + 1] = iux;
}
}
}
//Aquí cargamos el mapa ordenado
LinkedHashMap<Character, Integer> o = new LinkedHashMap();
for (int i = 0; i < apariciones.length; i++) {
o.put(caracter[i], apariciones[i]);
}
return o;
}
public static void main(String[] args) {
String cadena = "Hola Cesar, soy yo Billy jaja... esta es una cadena y vamos a contar sus caracteres";
LinkedHashMap<Character, Integer> m = new LinkedHashMap();
for (char c : cadena.toCharArray()) {
m.put(c, m.containsKey(c) ? m.get(c) + 1 : 1);
}
LinkedHashMap<Character, Integer> ordenado = ordenar(m);
System.out.println("A continuación el conteo");
ordenado.forEach((k, v) -> {
System.out.println(k + ": " + v);
});
}
}