Doubt - HashMap

0

I need to go through a list of hotel reservations and, for each such reservation, capture the Event that is associated with it. At the end of the process I need to say how many hotel reservations each event has and finally play the result on the screen. The idea I had was to use Map . But I'm having trouble mapping the amount of reservations for each event with the corresponding event.

The idea is:

Evento  Quantidade de Reservas
X                 10
Y                 15

What I have done so far is the following:

int hotelReservationQtd = 0;
List<IHotelReservation> iHotelReservationList = Lists.newArrayList();

iHotelReservationList.addAll(this.hotelReservationDao.listAll());
iHotelReservationList.addAll(this.omnibeesHotelReservationDao.listAll());

Map<Event, Integer> hotelReservationMap = Maps.newHashMap();

for (IHotelReservation iHotelReservation: iHotelReservationList) {
    if (hotelReservationMap.containsKey(iHotelReservation.getEvent())) {
        hotelReservationMap.put(iHotelReservation.getEvent(), hotelReservationQtd++);
    }
}

this.result.include("hotelReservationMap", hotelReservationMap.values());
    
asked by anonymous 24.06.2017 / 18:51

1 answer

0

I think the code below does what you want, the main change was in for . In the end, the hotelReservationMap map will have an Event as key and an Integer (> = 1) as Value for each key, counting how many times an Same Event appeared in loop iterations:

List<IHotelReservation> iHotelReservationList = Lists.newArrayList();

iHotelReservationList.addAll(this.hotelReservationDao.listAll());
iHotelReservationList.addAll(this.omnibeesHotelReservationDao.listAll());

Map<Event, Integer> hotelReservationMap = Maps.newHashMap();

for (IHotelReservation iHotelReservation : iHotelReservationList) {
    int hotelReservationQtd = 1;
    if (hotelReservationMap.containsKey(iHotelReservation.getEvent())) {
        hotelReservationQtd = hotelReservationMap.get(iHotelReservation.getEvent())+1;
    }
    hotelReservationMap.put(iHotelReservation.getEvent(), hotelReservationQtd);
}

this.result.include("hotelReservationMap", hotelReservationMap.values());

To test the working logic, I created a code that works similarly, it gets a list of Pessoa objects and counts how many people have the same name, generating a map that has a nome as key and has as value a Integer that says how many people has this nome .

public class ContadorDeRepeticaoDeNomes {

    public static void main(String[] args) {
        List<Pessoa> listaDePessoas = new ArrayList<>();

        //Duas pessoas com o Nome João
        listaDePessoas.add(new Pessoa("João"));
        listaDePessoas.add(new Pessoa("João"));

        //Uma pessoa com o nome "Pedro"
        listaDePessoas.add(new Pessoa("Pedro"));

        //Três pessoas com o nome "Maria"
        listaDePessoas.add(new Pessoa("Maria"));
        listaDePessoas.add(new Pessoa("Maria"));
        listaDePessoas.add(new Pessoa("Maria"));

        //Uma pessoa com o nome "José"
        listaDePessoas.add(new Pessoa("José"));

        Map<String, Integer> nomesDePessoas = contarPessoasComMesmoNome(listaDePessoas);

        nomesDePessoas.forEach((k,v) -> System.out.println("Existem "+v+" Pessoas com o Nome "+k));

    }

    /**Retorna um Map com um "Nome" como chave, e, a quantidade de Pessoas que tem este nome como "Value"*/
    private static Map<String, Integer> contarPessoasComMesmoNome(List<Pessoa> listaDePessoas) {
        Map<String, Integer> nomesDePessoas = new HashMap<>();

        for (Pessoa pessoa : listaDePessoas) {
            int quantidadeDePessoasComEsteNome = 1;
            if (nomesDePessoas.containsKey(pessoa.nome)) {
                quantidadeDePessoasComEsteNome = nomesDePessoas.get(pessoa.nome)+1;
            }
            nomesDePessoas.put(pessoa.nome, quantidadeDePessoasComEsteNome);
        }

        return nomesDePessoas;
    }

    private static class Pessoa {
        String nome;

        public Pessoa(String nome) {
            this.nome = nome;
        }
    }
}

Running it Output is as expected:

  

There are 2 People with the First Name João   There are 1 People with the Name José
  There are 1 People with the First Name Pedro   There are 3 People under the Name Maria

    
24.06.2017 / 20:45