How to compare the value of a Hashmap<key, value> with a variable?

Asked

Viewed 959 times

6

I have a HashMap aluno<Integer, Float>, where the key Integer will be the student’s registration number, and the amount Float will be the student’s grade.

I got the grade average using the Iterator, but now I need to know which students have the above average grade and I couldn’t do using the symbol >

follows 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 +  ", ");
    }
  • 5

    You have to start the iterator again because in the second while the iterator is at the end of Hasmap

  • 2

    @Jorgeb. already makes an answer

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

5

It doesn’t work because you have to start Iterator again because in the second while the 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 +  ", ");
}

5

First, you know why you chose the HashMap? Do you know that it has no set order? That you will not have the students in order of enrollment? This solves what you need?

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

We can roughly say that the iterator is an object that stores the variable of a loop (of course he’s more than that) and every time he goes to the next element he’s incremented.

So the solution is to restart the iterator. Since the Java iterator does not allow to do this directly, we have to start a new iterator. It would be something like that:

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 put in the Github for future reference.

  • You are late :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.