HashMap in java, error counting elements

0

I have this method that should return the name of the dish of a restaurant with the number of times it is in total of all restaurants. If the dish is returned 1, if it is more than once must count and return those times. but he's only giving me back 2 times and if I increase the amount he keeps counting 2. What's wrong?

class Restaurant {

String nombre;
String direccion;
int capacidad;
LinkedList<String> platosMenu;

public static HashMap<String, Integer> cuentaPlatos(LinkedList<Restaurante> 
 lista) {
    HashMap<String, Integer> mapa = new HashMap();
    ListIterator<Restaurante> it = lista.listIterator();
    int cont = 1; 
    while (it.hasNext()) {
        Restaurante clave = it.next();
        LinkedList<String> platos = clave.getPlatosMenu();
        for(String plato : platos){
            if(mapa.containsKey(plato)){
                mapa.put(plato,cont+1);
            }else{
                mapa.put(plato,cont);
            }}}
    return mapa;

Main --------------

    Restaurante{nombre=Restaurante1, direccion=Sur, capacidad=50, 
    platosMenu=[guatita, encebollado, arroz con menestra]}

    Restaurante{nombre=Restaurante2, direccion=Centro, capacidad=85, 
    platosMenu=[encebollado, arroz con menestra, caldo de bola]}

    Restaurante{nombre=Restaurante3, direccion=Este, capacidad=100, 
    platosMenu=[guatita, pescado frito, encebollado, arroz con menestra]}

    Retorno:
    ( encebollado y arroz con menestra deberian retornar 3 )
    guatita - 2
    encebollado - 2
    caldo de bola - 1
    arroz con menestra - 2
    pescado frito - 1
    
asked by Michelle 21.11.2017 в 00:20
source

2 answers

1

It is obvious that your intention is to go adding 1 to the value that you keep on your map. But in reality, that is not what you do. The problem is that you never read the value of the map, you only assign it cont or cont + 1 , which equals 1 or 2 . Note that apart from the int cont = 1; statement, you never assign a value to the variable cont .

If we simplify the code and apply what was your intention to accumulate the account, it can be achieved in the following way:

public static HashMap<String, Integer> cuentaPlatos(LinkedList<Restaurante> lista) {
    HashMap<String, Integer> mapa = new HashMap();
    for (Restaurante clave : lista) {
        for (String plato : clave.getPlatosMenu()) {
            int cont = mapa.getOrDefault(plato, 0) + 1; // 1. leer
            mapa.put(plato, cont);                      // 2. luego asignar
        }
    }
    return mapa;
}
    
answered by 21.11.2017 / 02:42
source
1

You are never increasing the value of your variable 'cont' beyond 2. When you declare it has value 1. Then, every time there is a match you do cont + 1 (which is equal to 2). In case there is a third iteration of the loop, the value of 'cont' is still 1. You should increase its value: cont + = 1 ...

  if(mapa.containsKey(plato)){ 
                cont += 1;
                mapa.put(plato,cont);
            }
    
answered by 21.11.2017 в 01:17