I saw the following code in the project (Spring):

manRepository.findAll().forEach(mans::add); 

What kind of interesting forEach?
I only know this:

 for(int i: list(массив или коллекция)){ //тело цикла } 
  • This Stream API - gil9red

1 answer 1

This is forEach for collections, which takes lambda, and lambda itself can be replaced by a reference to the method

 List<Integer> ints = new ArrayList(); ints.add(1); ints.add(2); List<Integer> copy = new ArrayList(); ints.forEach(copy::add); ints.forEach(oneIntFromInts -> copy.add(oneIntFromInts)); //эквивалент строке выше copy.forEach(oneIntFromCopy -> System.out.println(oneIntFromCopy));