[{"content":"Why it matters IntStream.range(0, n).forEach(...) takes an IntConsumer, which cannot throw checked exceptions. The usual workaround wraps and unwraps.\nSTREAMS IntStream.range(0, 3).forEach(i -\u003e { try { Files.createFile(path(i)); } catch (IOException e) { throw new UncheckedIOException(e); } }); JAVA-REPEAT repeat(3, i -\u003e Files.createFile(path(i))); // throws IOException How the type is inferred repeat is declared \u003cE extends Throwable\u003e ... throws E. The compiler picks E from what the lambda throws:\nThrows nothing checked: E is RuntimeException; nothing to declare. Throws one checked type: E is that type, for example IOException. Throws several: E is their common supertype, often Exception. Catch inside the action if you need them apart. A failure stops the loop repeat(5, i -\u003e { ran.add(i); if (i == 1) throw new IOException(\"ERROR!\"); }); // ran == [0, 1]; the IOException reaches the caller 0ran 1threw 2skipped 3skipped 4skipped Keep going after a failure To run every iteration regardless, catch inside the action and collect what failed.\nList\u003cIOException\u003e failures = new ArrayList\u003c\u003e(); repeat(5, i -\u003e { try { Files.createFile(path(i)); } catch (IOException e) { failures.add(e); } }); ","lede":"The action can throw a checked exception. It reaches the caller unchanged, and the loop stops.","section":"GUIDES","title":"Checked exceptions","url":"/guides/checked-exceptions/"},{"content":" Breaking change. The new repeat returns void; it no longer collects results into a list. Steps 1 Bump the version\n\u003cversion\u003e0.0.9 1.0.1\u003c/version\u003e 2 Switch to the static import\nimport io.github.repeat.Repeat; import static io.github.repeat.Repeat.*; 3 Rewrite each call\nResult ignored\nRepeat.of().times(3).repeat(i -\u003e { send(i); return null; }); repeat(3, i -\u003e send(i)); Result used: collect it yourself\nList\u003cString\u003e ids = Repeat.of().times(3).repeat(i -\u003e \"id-\" + i); List\u003cString\u003e ids = new ArrayList\u003c\u003e(); repeat(3, i -\u003e ids.add(\"id-\" + i)); Removed types 0.0.9 1.0.0 and later Repeat.of() → RepeatContext Repeat.repeat(int, IndexedAction) RepeatCallback\u003cT, E\u003e IndexedAction\u003cE\u003e (no return value) doWithRepeat(int) accept(int) RepeatOperations, RepeatTemplate Removed; no replacement needed ","lede":"1.0.0 replaces the builder with one static method. 0.0.9 and earlier used Repeat.of().times(n).repeat(...).","section":"GUIDES","title":"Migrating from 0.x","url":"/guides/migrating-from-0x/"},{"content":"Summary Type Member Description Repeat repeat(int, IndexedAction\u003cE\u003e) Runs the action times times with the zero-based index. IndexedAction\u003cE\u003e accept(int) The action your lambda implements; may throw E. Repeat final class since 1.0.0\nKotlin-style function for running code multiple times. Designed for static import. Holds only static members; it cannot be instantiated.\npublic final class Repeat repeat public static \u003cE extends Throwable\u003e void repeat( final int times, final IndexedAction\u003cE\u003e action) throws E Executes the action the given number of times, passing the zero-based index of each iteration. Like Kotlin’s repeat, a zero or negative times runs nothing.\nTYPE PARAMETERS E — the exception type the action may throw; inferred from the lambda. PARAMETERS times — the number of times to execute the actionaction — the action to execute; receives the index THROWS E — if the action fails; remaining iterations are not executed Behavior When Result times \u003e 0 Calls action.accept(i) for i = 0 … times - 1, in order, on the calling thread. times \u003c= 0 Does nothing. The action is never called. action throws at index k Indexes 0 … k ran. The exception propagates unchanged, not wrapped. action == null NullPointerException on the first iteration; nothing if times \u003c= 0. Examples import static io.github.repeat.Repeat.*; // method reference: collect the indexes 0, 1, 2 List\u003cInteger\u003e indexes = new ArrayList\u003c\u003e(); repeat(3, indexes::add); // ignore the index StringBuilder out = new StringBuilder(); repeat(2, i -\u003e out.append(\"x\")); // \"xx\" View sourceJavadoc: Repeat\nIndexedAction\u003cE\u003e functional interface since 1.0.0\nAn action that receives the zero-based index of the current iteration. You rarely name it: a lambda or method reference passed to repeat becomes one.\n@FunctionalInterface public interface IndexedAction\u003cE extends Throwable\u003e TYPE PARAMETERS E — the exception type accept may throw. For a lambda that throws nothing checked, the compiler infers RuntimeException, so callers declare nothing. accept void accept(int index) throws E PARAMETERS index — zero-based index of the current iteration THROWS E — any exception the action raises Ways to write one Form Example E is Lambda i -\u003e log.info(\"run \" + i) RuntimeException Method reference indexes::add RuntimeException Throwing lambda i -\u003e Files.delete(paths[i]) IOException Stored in a variable IndexedAction\u003cIOException\u003e a = i -\u003e ...; as declared View sourceJavadoc: IndexedAction\n","lede":"Package io.github.repeat: one class, one functional interface. Generated Javadoc for every version is on javadoc.io.","section":"API REFERENCE","title":"API reference","url":"/api/"},{"content":"Contributors java-repeat is built by its contributors on GitHub. Issues and pull requests are welcome in java-repeat/java-repeat.\nInspiration The API follows Kotlin’s repeat function: same name, same zero-based index, same no-op for a zero or negative count.\nThis site Hugo: static site generator. JetBrains Mono: monospaced type for headings and code. Hosted on GitHub Pages from java-repeat/java-repeat.github.io. ","lede":"The people and tools behind java-repeat.","section":"PROJECT","title":"Credits","url":"/credits/"},{"content":"Requirements Java 8 or later. No runtime dependencies. Install Maven \u003cdependency\u003e \u003cgroupId\u003eio.github.java-repeat\u003c/groupId\u003e \u003cartifactId\u003erepeat\u003c/artifactId\u003e \u003cversion\u003e1.0.1\u003c/version\u003e \u003c/dependency\u003e Gradle implementation(\"io.github.java-repeat:repeat:1.0.1\") Your first call import static io.github.repeat.Repeat.*; repeat(3, i -\u003e System.out.println(\"attempt \" + i)); Output\nattempt 0 attempt 1 attempt 2 The lambda receives the zero-based index of each iteration. A zero or negative count does nothing.\nThrowing checked exceptions The action may throw. The exception type is inferred, so the caller declares it as if it had written the loop itself.\nvoid createFiles() throws IOException { repeat(3, i -\u003e Files.createFile(Paths.get(\"file-\" + i))); } If an iteration throws, the remaining iterations do not run. See Checked exceptions. Next API reference: the full contract of repeat(int, IndexedAction). Migrating from 0.x: if you used Repeat.of().times(n). ","lede":"Add one dependency, statically import Repeat.*, and call repeat.","section":"DOCS","title":"Getting started","url":"/getting-started/"}]