Doubt - Hashmap

Asked

Viewed 54 times

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 difficulty 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’ve done so far is this::

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());
  • This way of doing it reminded me of Ollection Multiset from the Guava library. Consider this Ollection for your case, if it seems appropriate to you.

1 answer

0


I believe the code below does what you want, the main change was in the for. At the end, the map hotelReservationMap will have an Event as key and an Integer (>=1) as Value for each key, counting how many times a 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 in a similar way, it receives a list of objects Pessoa and counts how many people have the same name, generating a map that has a nome as key and has as value a Integer who says how many people have 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;
        }
    }
}

When executing it the Output is as expected:

There are 2 People with the Name John
There are 1 People with the Name José
There are 1 People Named Pedro
There are 3 People Named Maria

Browser other questions tagged

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