Vadzim Vadzim - 1 year ago 275
Java Question

How to iterate with foreach loop over java 8 stream

Suppose we try to apply to java 8 stream a lambda that could throw checked exception:

Stream<String> stream = Stream.of("1", "2", "3");
Writer writer = new FileWriter("example.txt");

stream.forEach(s -> writer.append(s)); // Unhandled exception:

This won't compile.

One workaround is to nest checked exception in
but it complicates later exception handling and it's just ugly:

stream.forEach(s -> {
try {
} catch (IOException e) {
throw new RuntimeException(e);

Alternative workaround could be to convert limited functional
to plain old foreach loop that is more friendly to checked exceptions.

But naive approaches fail:

for (String s : stream) { // for-each not applicable to expression type '<java.lang.String>'

for (String s : stream.iterator()) { // foreach not applicable to type 'java.util.Iterator<java.lang.String>'


A trick that answers this question was previosly posted at Why does Stream<T> not implement Iterable<T>? as side answer that doesn't really answer that question itself.
I think this is not enough to qualify this question as duplicate of that one because they ask different things.

Answer Source

By definition foreach loop requires an Iterable to be passed in.

It can be achieved with anonymous class:

    for (String s : new Iterable<String>() {
        public Iterator<String> iterator() {
            return stream.iterator();
    }) {

This can be simplified with lambda because Iterable is a functional interface:

    for (String s : (Iterable<String>) () -> stream.iterator()) {

This can be converted to method reference:

    for (String s : (Iterable<String>) stream::iterator) {

Explicit cast can be avoided with intermediate variable or method param:

    Iterable<String> iterable = stream::iterator;
    for (String s : iterable) {

There is also StreamEx library in maven central that features iterable streams and other perks out of the box.

Here is some most popular questions that provide workarounds on checked exception handling within lambdas and streams:

Java 8 Lambda function that throws exception?

Java 8: Lambda-Streams, Filter by Method with Exception

How can I throw CHECKED exceptions from inside Java 8 streams?

Java 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?

Kotlin ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download