Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a method multiple times using a lambda expression in Java

What is the easiest and quickest way to execute a code block multiple times using a lambda expression in Java 8? For example, a code that will replace the following:

for (int i = 0; i < 20; i ++) {
 doSomething();
} 
like image 410
Omri Avatar asked Mar 22 '15 17:03

Omri


1 Answers

You can use an IntStream.range, but I don't see much advantage to this approach over the loop you are already using.

IntStream.range(0,20).forEach(i -> doSomething());
like image 64
Eran Avatar answered Oct 09 '22 06:10

Eran