repeat(n, i -> ...)

Kotlin-style repetition for Java, with support for checked exceptions.

import static io.github.repeat.Repeat.*;

// run an action n times
repeat(3, i -> System.out.println("attempt " + i));

// checked exceptions propagate without wrapping
repeat(3, i -> Files.createFile(Paths.get("file-" + i)));
// throws IOException

Install

One dependency, no transitive dependencies. Java 8 or later.

<dependency>
  <groupId>io.github.java-repeat</groupId>
  <artifactId>repeat</artifactId>
  <version>1.0.1</version>
</dependency>

If you know Kotlin

Same name, same index, same rules. As in Kotlin, a zero or negative count does nothing.

KOTLINJAVA-REPEAT
repeat(n) { i -> ... }repeat(n, i -> ...)
repeat(0) { ... }repeat(0, i -> ...) // no-op

Documentation