A question with Hashmap

Asked

Viewed 183 times

0

I made a simple code to register a person in a HashMap. Pessoa is a class that has name and age, but is not giving to register more than one person. Every time I show her, she shows only the first one I’ve ever registered.

My code:

import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;

    public class Main {

        public static void main(String[] args){
            int op;
            String nome;
            int ide;
            int j = 0;
            Scanner sc = new Scanner(System.in);
            Map<Integer,Pessoa>Mostra = new HashMap<Integer,Pessoa>();
            while(true){
                System.out.println("1-Cadastrar");
                System.out.println("2-Mostrar todas");
                op = sc.nextInt();
                if(op == 1) {
                    if(Mostra.isEmpty()) {
                        System.out.println("Nome");
                        nome = sc.next();
                        System.out.println("idade");
                        ide = sc.nextInt();
                        Pessoa p = new Pessoa(nome,ide);
                        Mostra.put(1,p);
                    }else {
                        System.out.println("Nome");
                        nome = sc.next();
                        System.out.println("idade");
                        ide = sc.nextInt();
                        Pessoa p = new Pessoa(nome,ide);
                        for(int i = 0; i < Mostra.size(); i++) {
                            j++;
                        }
                        Mostra.put(j,p);
                    }
                }else if(op == 2) {
                    for(int i = 0; i < Mostra.size(); i++) {
                        System.out.println(Mostra.get(i));
                    }
                }
            }
        }

    }
  • Are you using Java 7 or higher? If yes, you can use diamond operator, it makes it more elegant to instantiate a new HashMap<>() than a new HashMap<Integer, Pessoa>

  • And your code is not suitable for the use of HashMap, would be much better with lists...

  • I know it would be better to use lists, but I have proof tomorrow and I don’t know how to use the right hashmap

  • thus? Map Shows = new Hashmap<>();

1 answer

4


Let’s simplify your code.

When you have something like this:

if (x) {
    a;
    b;
    c;
    d1;
} else {
    a;
    b;
    c;
    d2;
}

Assuming the evaluation of x does not produce side effects in a, b or c or vice versa, you could simplify this code to look like this:

a;
b;
c;
if (x) {
    d1;
} else {
    d2;
}

This is the case of the lines that instantiate Pessoa.

As you are preparing for a proof, you should know that the naming convention says that variable names should be lowercase. Therefore, use Mostra instead of mostra.

And we also have it from here:

                    for(int i = 0; i < Mostra.size(); i++) {
                        j++;
                    }
                    Mostra.put(j,p);

The fact that you wear i in the header of for and j in the body is wrong and that’s the mistake that makes your code not work. However, use a for to do this alone is already wrong, as it is the equivalent of counting on your fingers when you have a calculator at your disposal. I think what you wanted was just this:

                   mostra.put(mostra.size() + 1, p);

With these changes, we’ll have a code like this:

                if (mostra.isEmpty()) {
                    mostra.put(1, p);
                } else {
                    mostra.put(mostra.size() + 1, p);
                }

What would happen if the case of if had entered the else? We’ll have to mostra.size() will give zero, and added with 1, will give 1, which is exactly what there is in the body of if. Therefore, we can eliminate the if.

And also, since the idea is to learn the use of Map, you can learn to use it with the Enhanced-for without needing an accountant for that:

                for (Pessoa p : mostra.values()) {
                    System.out.println(p);
                }

Declaring the variable in the smallest possible scope only when using it is also usually a good idea.

Your code goes like this:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer, Pessoa> mostra = new HashMap<>();
        while (true) {
            System.out.println("1-Cadastrar");
            System.out.println("2-Mostrar todas");
            int op = sc.nextInt();
            if (op == 1) {
                System.out.println("Nome");
                String nome = sc.next();
                System.out.println("idade");
                int idade = sc.nextInt();
                Pessoa p = new Pessoa(nome, idade);
                mostra.put(mostra.size() + 1, p);
            } else if (op == 2) {
                for (Pessoa p : mostra.values()) {
                    System.out.println(p);
                }
            }
        }
    }

}
  • thank you very much, that’s right

Browser other questions tagged

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