Doubt - HashMap & List

0

My need was as follows: A report that would bring the Event and the number of hotel reservations that were associated with that Event. And so it was followed as shown in the code below:

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

    iHotelReservationList.addAll(this.hotelReservationDao.listByReservationStatus(hotelReservationFilter));
    iHotelReservationList.addAll(this.omnibeesHotelReservationDao.listByReservationStatus(hotelReservationFilter));

    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);
}

I still need to return a Map using Event as the key. But now with the number of Allocations given that Event, no more than HotelReservations. It is worth mentioning that I have a List getAllocationList () in IHotelReservation.

    
asked by anonymous 26.06.2017 / 19:44

1 answer

0

Some minor code changes are enough to change what code causes it to say how many Allocations are associated with each Event rather than how many IHotelReservations are associated with each Event.

There are comments on the important things in the code to explain them.

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

iHotelReservationList.addAll(this.hotelReservationDao.listByReservationStatus(hotelReservationFilter));
iHotelReservationList.addAll(this.omnibeesHotelReservationDao.listByReservationStatus(hotelReservationFilter));

//A variável segue um padrão meu de nomenclatura para maps, que é "nomeDobjetoDaKey_NomeDoObjetoDoValue", ou seja, na key temos um "evento" e para este evento temos no Value o "TotalDeAllocationsAssociadosAoEvento"
Map<Event, Integer> evento_TotalDeAllocationsAssociadosAoEvento = Maps.newHashMap();

for (IHotelReservation iHotelReservation : iHotelReservationList) {
    //Na linha abaixo colocamos em "allocationsQtd" o total de Allocations do iHotelReservation da iteração atual do loop
    int allocationsQtd = iHotelReservation.getAllocationList().size(); //tenha certeza de que "getAllocationList()" não retornará null, caso contrário, vc terá que tratar isso
    if (evento_TotalDeAllocationsAssociadosAoEvento.containsKey(iHotelReservation.getEvent())) {
        //Entramos no if se já colocamos o Event no map, e nesse caso, vamos incrementar allocationsQtd com a quantidade de Allocations que já estava no map
        allocationsQtd += evento_TotalDeAllocationsAssociadosAoEvento.get(iHotelReservation.getEvent());
    }
    //Na linha abaixo iserimos o Event pela primeira vez no map, ou, atualizamos o valor dele no map
    evento_TotalDeAllocationsAssociadosAoEvento.put(iHotelReservation.getEvent(), allocationsQtd);
}
this.result.include("evento_TotalDeAllocationsAssociadosAoEvento", evento_TotalDeAllocationsAssociadosAoEvento);
    
26.06.2017 / 21:39