How to use a counter within a HashMap?

4

Is it possible to do this?

public static void main(String[] args) {

    Scanner in = new Scanner (System.in);

    Map <String,Integer> mapa = new HashMap <String,Integer>();

    mapa.put("45 - Jose"  , ? ); //A "Key" deve ser o numero do Candidato,        
    mapa.put("13 - Maria" , ? ); // e  o "Value" o contador  que vai 
    mapa.put("20 - Pedro" , ? ); // contabilizar os votos,  porem nao sei
    mapa.put("50 - Carol" , ? ); // como criar  o contador aqui.      

    for (Map.Entry <String , Integer> conteudo : mapa.entrySet()){

        System.out.println(conteudo.getKey());

        System.out.printf("Votos = %d \n",conteudo.getValue());
    }

    String key, continuar;

    do{

    System.out.println("\nEm que candidato deseja votar? ");
    key =  in.nextLine();

    if (mapa.containsKey(key)){
        mapa.replace(key, new Integer (?));

        System.out.printf("\nCandidato: %s \nVotos = %d \n", key, mapa.get(key));  
    }else{
        System.err.printf("\nCandidato %s nao encontrado.\n" ,key);
    }

    System.out.println("\nDeseja continuar: (s/n)");
    continuar = in.nextLine();

    }while("s".equalsIgnoreCase(continuar));
    
asked by anonymous 16.05.2016 / 04:36

1 answer

4

To initialize the counter simply set zero, as you normally would. To increment you have to get the current value and you already know that you do this with the method get() and to put the new value you already know that it is with put() , you only need to join the two, obviously adding one to the value found before saving the new, as is always done in any counter. That is, there is nothing I did not know how to do.

import java.util.*;
class Ideone {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        Map<String,Integer> mapa = new HashMap<String,Integer>();
        mapa.put("45 - Jose"  , 0); //A "Key" deve ser o numero do Candidato,        
        mapa.put("13 - Maria" , 0); // e  o "Value" o contador  que vai 
        mapa.put("20 - Pedro" , 0); // contabilizar os votos,  porem nao sei
        mapa.put("50 - Carol" , 0); // como criar  o contador aqui.      
        for (Map.Entry<String, Integer> conteudo : mapa.entrySet()) {
            System.out.println(conteudo.getKey());
            System.out.printf("Votos = %d \n", conteudo.getValue());
        }
        String continuar;
        do {
            System.out.println("\nEm que candidato deseja votar? ");
            String key =  in.nextLine();
            if (mapa.containsKey(key)) {
                mapa.put(key, mapa.get(key) + 1);
                System.out.printf("\nCandidato: %s \nVotos = %d \n", key, mapa.get(key));  
            } else {
                System.err.printf("\nCandidato %s nao encontrado.\n", key);
            }
            System.out.println("\nDeseja continuar: (s/n)");
            continuar = in.nextLine();
        } while ("s".equalsIgnoreCase(continuar));

    }
}

See working on ideone and on CodingGround .

It would also be nice to maintain consistency in the organization of the code.

    
16.05.2016 / 13:22