A doubt with HashMap

1

I made a simple code to register a person with a HashMap . Pessoa is a class that has name and age, but is not giving to register more than one person. Whenever I'm going to show it, it shows only the first one I 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));
                    }
                }
            }
        }

    }
    
asked by anonymous 21.06.2018 / 04:43

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 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 lines that instantiate Pessoa .

As you are preparing for a test, 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 use i in the for and j header in the body is wrong and this is the error that causes your code to not work . However, using for to do this by itself 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 simply this:

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

With these changes, we will have a code like this:

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

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

Also, since the idea is to learn how to use Map , you can learn how to use it with enhanced-for without needing a counter for it:

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

Declaring the variable in the smallest possible scope only when you use it is often a good idea.

Your code looks 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);
                }
            }
        }
    }

}
    
21.06.2018 / 05:05