Fundamentals

Default Methods

Collections

Idioms and Techniques

Design Rationale

Advanced Questions

How can I use Streams to repeat an action a certain number of times?

Yes. You can do this to call a method n times:

IntStream.range(0, n).forEach(i -> doSomething());

By itself this isn’t terribly interesting. But if you put it into a little helper function that takes a couple parameters,

void repeat(int count, Runnable action) {
    IntStream.range(0, count).forEach(i -> action.run());
}

This will enable you to do things like this:

repeat(3, () -> System.out.println("Hello!"));

and also this:

repeat(4, this::doSomething);