How to compare the value of a HashMapchave, value with a variable?

5

I have a HashMap aluno<Integer, Float> , where the key Integer will be the student's enrollment number, and the Float value will be the student's grade.

I got the grades average using Iterator , but now I need to know which students have the grade above the average and could not do it using the > symbol

follow the code:

  HashMap<Integer, Float> aluno = new HashMap<Integer Float>();
  .
  .
  .
  //achei soma das notas
  Iterator<Integer> i = aluno.keySet().iterator();
  while(i.hasNext()){
      int chave = i.next();
      sumNotas+=aluno.get(chave);
  }

  //printei média
  //n é a quantidde de alunos
  System.out.println("média: " + sumNotas/n + "\n" + "Alunos acima da média:" + "\n");

  //quero printar se a nota do aluno é maior que a média, mas não funciona
  //n é a quantidde de alunos
  while(i.hasNext()){
    int chave = i.next();
    if(aluno.get(chave) > sumNotas/n)
        System.out.print(chave +  ", ");
    }
    
asked by anonymous 20.11.2014 / 15:58

2 answers

5

It does not work because you have to start Iterator again because in the second while Iterator is at the end of HashMap :

//quero printar se a nota do aluno é maior que a média, mas não funciona
//n é a quantidde de alunos

i = aluno.keySet().iterator();
while(i.hasNext()){
    int chave = i.next();
    if(aluno.get(chave) > sumNotas/n)
        System.out.print(chave +  ", ");
}
    
20.11.2014 / 16:21
4

First: Do you know why you chose HashMap ? Do you know that he has no definite order? That you will not have the students in order of matriculation? This solves what you need?

Your problem is that to scan the entire map is done through an iterator which is nothing more than a means of knowing where you are in a collection of data. It knows where you are positioned in the collection. Of course, one of these collections is HashMap . If you browse the entire collection, what happens when you try to navigate it with the same iterator? He can not. She thinks it's over, there's nothing left to see there.

We can say roughly that the iterator is an object that holds the variable of a loop (of course it is more than this) and each time it goes to the next element it is incremented .

Then the solution is to restart the iterator. Since the Java iterator does not allow doing this directly, we have to start a new iterator. It would look something like this:

HashMap<Integer, Float> aluno = new HashMap<Integer Float>();
  .
  .
  .
  //achei soma das notas
  Iterator<Integer> i = aluno.keySet().iterator();
  while(i.hasNext()){
      int chave = i.next();
      sumNotas+=aluno.get(chave);
  }

  //printei média
  //n é a quantidde de alunos
  System.out.println("média: " + sumNotas/n + "\n" + "Alunos acima da média:" + "\n");

  //quero printar se a nota do aluno é maior que a média, mas não funciona
  //n é a quantidde de alunos
  i = aluno.keySet().iterator(); //<============ adicionado aqui
  while(i.hasNext()){
    int chave = i.next();
    if(aluno.get(chave) > sumNotas/n)
        System.out.print(chave +  ", ");
    }

I placed GitHub for future reference .

    
20.11.2014 / 16:21