How to take the last element of a list in java?

0

I have the following list

listNivelesPersona2 = nivelesPersonaService.findNivelesPersonaConsultaHorario(cvePersona);

I want to recover the last record of the time of entry and exit, I do the following

    for (NivelesPersona nivelesPersona : listNivelesPersona2) {
        System.out.println("HORARIO" + nivelesPersona.getHorarios().getHorarioEntrada() + " " +nivelesPersona.getHorarios().getHorarioSalida());
    }

and it shows me all the entry and exit times that person has

HORARIO11:30 18:00
HORARIO12:00 19:00
HORARIO15:00 20:00

I always want to take the last record, which is the newest one, in this case I'm only interested in the one from 15:00 to 20:00, to show it in view, how can I get that last record?

    
asked by Root93 09.04.2018 в 20:23
source

1 answer

1

To get the last record of a list you just have to know how many elements are on the list, then remove the last item from the list, example,

NivelesPersona nivelesPersona = listNivelesPersona2.get(listNivelesPersona2.size() -1 );

the get function brings me the value in the position that we indicate, and the size function returns the total sum of the elements in the list, so if the list has 10 records when with the size function it prints me 10 and with the get function I tell him to bring me the value that is in position 10 that would be the last of the list. you are already getting the last record in the list.

    
answered by 09.04.2018 в 21:20