import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
class ContaLetras {
public static void main
(String[] args
) { Map
<String, Integer
> ocorrencias
;
ocorrencias = contar("Vê se tira notas boas!");
ocorrencias.
forEach((chave, valor
) -> System.
out.
print(chave
+ ":" + valor
+ " ")); }
Map
<String, Integer
> resultado
= new TreeMap
<>(); // TreeMap para manter o Map ordenado pelas chaves List<String> ocorrencias;
Set<String> letras;
ocorrencias
= Arrays.
asList(frase.
replace(" ",
"").
split("")); // Transforma a frase em uma lista que facilitará a contagem letras = new TreeSet<>(ocorrencias); // Pega as letras sem duplicidade
// Percorre o array de letras sem repetição contando as ocorrências
letras.
forEach((String letra
) -> { resultado.
put(letra,
Collections.
frequency(ocorrencias, letra
)); });
return resultado;
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CmltcG9ydCBqYXZhLnV0aWwuQ29sbGVjdGlvbnM7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5NYXA7CmltcG9ydCBqYXZhLnV0aWwuU2V0OwppbXBvcnQgamF2YS51dGlsLlRyZWVNYXA7CmltcG9ydCBqYXZhLnV0aWwuVHJlZVNldDsKCmNsYXNzIENvbnRhTGV0cmFzIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgIE1hcDxTdHJpbmcsIEludGVnZXI+IG9jb3JyZW5jaWFzOwoKICAgICAgb2NvcnJlbmNpYXMgPSBjb250YXIoIlbDqiBzZSB0aXJhIG5vdGFzIGJvYXMhIik7CiAgICAgIG9jb3JyZW5jaWFzLmZvckVhY2goKGNoYXZlLCB2YWxvcikgLT4gU3lzdGVtLm91dC5wcmludChjaGF2ZSArICI6IiArIHZhbG9yICsgIiAiKSk7CiAgICB9CgogICAgcHJpdmF0ZSBzdGF0aWMgTWFwPFN0cmluZywgSW50ZWdlcj4gY29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