Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop list object and getting it's element by index?

Tags:

java

java-8

I'm trying to migrate a function from java 7 to java8 but i'm stucked over getting the value of the indexed element while looping a list. What is the good way to do this ?

here is the code that i'm trying to migrate:

List<Employe> listEmploye = new ArrayList<>();
for(int i=0; i< ids.size();i++)
{
  Long idLong = Long.valueOf(ids.get(i));
  BigDecimal idBig= BigDecimal.valueOf(idLong);
  listEmploye.add(findByIdPointage(idBig));
}
like image 261
Hamza Khadhri Avatar asked Jan 23 '19 12:01

Hamza Khadhri


People also ask

How do you iterate through a list of objects?

Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true. Within the loop, obtain each element by calling next().


1 Answers

You can use Stream API to map your collection:

List<Employe> listEmploye  = ids.stream()
.map(Long::valueOf)
.map(BigDecimal::valueOf)
.map(this::findByIdPointage)
.collect(Collectors.toList());
like image 194
Beri Avatar answered Oct 06 '22 00:10

Beri