Getting started

Add one dependency, statically import Repeat.*, and call repeat.

Requirements

  • Java 8 or later.
  • No runtime dependencies.

Install

Maven

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

Gradle

implementation("io.github.java-repeat:repeat:1.0.1")

Your first call

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

repeat(3, i -> System.out.println("attempt " + i));

Output

attempt 0
attempt 1
attempt 2

The lambda receives the zero-based index of each iteration. A zero or negative count does nothing.

Throwing checked exceptions

The action may throw. The exception type is inferred, so the caller declares it as if it had written the loop itself.

void createFiles() throws IOException {
    repeat(3, i -> Files.createFile(Paths.get("file-" + i)));
}
If an iteration throws, the remaining iterations do not run. See Checked exceptions.

Next

Edit this page on GitHub