(Testa e depois comenta se funcionou ou não...)
infelizmente não era o que eu queria, mas obrigado pela ajuda, esse código ordenou pelos valores, mas só mostrou os valores. eu queria que mostrasse os pares chave=valor.
mas eu consegui, até que enfim, fazer algo, eu queria excluir a chave que tivesse menor valor, e acho queconsegui, depois de pensar muito.
sou muito iniciante, então queria que descem uma olhada no código e fizessem críticas, sei que tá ruim o código.
imagino que isso seja gambiarra, eu criei um objeto Letra que implementei comparator, que tem variaveis de instância chave e valor.
peguei as chaves do HashMap para criar os objetos Letra e inseri num PriorityQueue (que faz exatamente oque eu queria), depois é só eu excluir.
public class ExcluirMenor{
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
StringBuffer string = new StringBuffer();
PriorityQueue<Letra> fila = new PriorityQueue<Letra>();
public void contCaracteres(String fileName) {
try{
BufferedReader file = new BufferedReader(new FileReader(fileName));
String linha = null;
while((linha = file.readLine()) != null){
string.append(linha);
}
}catch (Exception e) {
e.printStackTrace();
}
//imprime um teste.
System.out.println(string);
for(int i=0; i<string.length(); i++){
if((map.get(string.charAt(i)) == null)){
map.put(string.charAt(i), 1);
}else{
map.put(string.charAt(i), map.get(string.charAt(i)) + 1);
}
}
//imprime um teste
System.out.println(map.toString());
for(Character character: map.keySet()){
fila.offer(new Letra(character, map.get(character)));
}
//imprime um teste
while(fila.size() > 0){
System.out.println(fila.peek().toString()+" eh esse óhh!!!");
fila.poll();
}
}
}
//classe Letra
public class Letra implements Comparable{
private Character chave;
private Integer valor;
public Character getChave() {
return chave;
}
public void setChave(Character chave) {
this.chave = chave;
}
public int getValor() {
return valor;
}
public void setValor(Integer valor) {
this.valor = valor;
}
public Letra(Character chave, Integer valor) {
super();
this.chave = chave;
this.valor = valor;
}
public String toString() {
return (chave+"="+valor);
}
public int compareTo(Object o) {
Letra letra = (Letra)o;
return this.valor.compareTo(letra.valor);
}
}
Obrigado!!