Doubt - Hashmap & List

Asked

Viewed 50 times

0

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

    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 key. But now with the number of Allocations given that Event, no more Hotelreservations. It is worth mentioning that I have a List getAllocationList() in Ihotelreservation.

  • See if I understand: "1 Event has 1-N Hotelreservetion", and, "1 Event has 1-N Allocation", is that it? It would not be better to add to Event a Hotelreservetion List and an Allocation Client (note, if it is not possible or undesirable to change the Event class, you can create an "Eventholder" that would have 1 Event and the Hotelreservetion and Allocation Lists of that Event)?

  • Event has no Hotelreservation. 1-N Hotelreservation has 1 Event. 1-N Allocation has 1 Hotelreservation. There is a lot of resistance to changing the Event class.

  • And what is the direct relationship between Event and Allocation? You simply want to add up the Allocations of all Hotelreservation that has a certain Event, doing this for each Event?

  • Exactly. The only relationship Allocation has with Event is Hotelreservation. I imagine you can go through the Hotelreservation list and pick up your Allocations.

  • And what does "hotel_reservation_id" have to be? I couldn’t see yet why it would be necessary to implement this functionality.

  • It is. Now talking I realized that I don’t need it because before I was imagining that I would have to go through a list of Allocations and compare the hotel_reservation_id with the Hotelreservation id. Do you understand ? But I just need to go through the list of Hotelreservation Allocations.

  • If you are already able to answer your question, answer it and accept the answer; otherwise you will need to edit your question: remove the business from the "hotel_reservation_id", show the "Ihotelreservation" method that returns the list of Allocations and tell what is type of return (is a List<Allocation>?). I imagine you want another Map<Event, Integer> evento_AllocationsAssociationsAoEvento , if that’s what triggers the question and explain what we talked about here in the comments.

  • I’m not able to answer my question yet and I edited it.

Show 3 more comments

1 answer

0


Some small code changes are enough to change what code it does so that it tells you how many Allocations are associated with each Event rather than how many Ihotelreservation 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.