534

I have an interface which returns java.lang.Iterable<T>.

I would like to manipulate that result using the Java 8 Stream API.

However Iterable can't "stream".

Any idea how to use the Iterable as a Stream without converting it to List?

6
  • 2
    If you can iterate, why not simly use a loop to check its condition or value or what so ever? Commented May 29, 2014 at 11:18
  • 35
    @AfzaalAhmadZeeshan because streams are much better Commented May 29, 2014 at 11:19
  • 2
    As I said, I need to do some maniuplations on that list (filters, mapping). I would like to use the new Java 8 JDK API - > Stream. but Iterable isnot "SteamAble" Commented May 29, 2014 at 11:19
  • 1
    Seems weird that myIterable.stream() does not exist! Commented Jun 15, 2018 at 14:15
  • 14
    @Guillaume: Yes, but Stream.of(iterable) produces Stream<Iterable<Object>>. Commented Oct 4, 2018 at 16:16

8 Answers 8

724
+300

Iterable has a spliterator() method, which you can pass to StreamSupport.stream to create a stream:

StreamSupport.stream(iterable.spliterator(), false)
             .filter(...)
             .moreStreamOps(...);

This is a much better answer than using spliteratorUnknownSize directly, as it is both easier and gets a better result. In the worst case, it's the same code (the default implementation uses spliteratorUnknownSize), but in the more common case, where your Iterable is already a collection, you'll get a better spliterator, and therefore better stream performance (maybe even good parallelism). It's also less code.

As you can see, getting a stream from an Iterable (see also this question) is not very painful.

Sign up to request clarification or add additional context in comments.

7 Comments

A static method on Stream would have been nice, e.g. Stream.ofIterable(iterable).
@robinst This wasn't an omission; it was a deliberate (and highly debated) choice. The challenge is that were this to exist, it would just be too easy to reach for it without understanding the tradeoffs that come with it. Because Iterable is so abstract, such a method would result in the worst-performing stream possible (no parallel support, no size information or characteristics (used to optimize execution choices)). Forcing more thought results in better APIs across the entire ecosystem. This was a tradeoff of "what's best for XYZ code" vs "what's best for all Java code."
Based on your explanation, I'm curious why we got Collection.stream() but not Iterable.stream(). It seems like the reasoning to omit Iterable.stream() (or Stream.ofIterable()) applies equally to Collection.stream().
@BrianGoetz It looks most of people are not in the level to understand you said above or don't care about. they only want to write the simple code by calling simple API. On the other hand, those things(parallel...) maybe not important to most of daily iterable operations.
|
99

If you can use Guava library, since version 21, you can use

Streams.stream(iterable)

2 Comments

Or, if you are stuck at an older version, use Lists.newArrayList(Iterable).
Current Guava implementation is not worse than accepted answer: github.com/google/guava/blob/master/guava/src/com/google/common/…
25

You can easily create a Stream out of an Iterable or Iterator:

public static <T> Stream<T> stream(Iterable<T> iterable) {
    return StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(
            iterable.iterator(),
            Spliterator.ORDERED
        ),
        false
    );
}

3 Comments

You have to write this function once and then just call it. Why does a call to stream(...) clutter your code?
Wanted to do it Inline short and elegant.. you right I can write this function once.. but I am into dropping code (and not adding code). anyway this answer is right because thats the way to convert this.
static import said function. short and elegant. (though not necessarily transparent)
11

I would like to suggest using JOOL library, it hides spliterator magic behind the Seq.seq(iterable) call and also provides a whole bunch of additional useful functionality.

1 Comment

Wow there is a whole JOO* family. I was only familiar with JOOQ. Thanks!
9

So as another answer mentioned Guava has support for this by using:

Streams.stream(iterable);

I want to highlight that the implementation does something slightly different than other answers suggested. If the Iterable is of type Collection they cast it.

public static <T> Stream<T> stream(Iterable<T> iterable) {
  return (iterable instanceof Collection)
    ? ((Collection<T>) iterable).stream()
    : StreamSupport.stream(iterable.spliterator(), false);
}

public static <T> Stream<T> stream(Iterator<T> iterator) {
  return StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(iterator, 0),
    false
  );
}

Comments

6

I've created this class:

public class Streams {
    /**
     * Converts Iterable to stream
     */
    public static <T> Stream<T>  streamOf(final Iterable<T> iterable) {
        return toStream(iterable, false);
    }

    /**
     * Converts Iterable to parallel stream
     */
    public static <T> Stream<T> parallelStreamOf(final Iterable<T> iterable) {
        return toStream(iterable, true);
    }

    private static <T> Stream<T> toStream(final Iterable<T> iterable, final boolean isParallel) {
        return StreamSupport.stream(iterable.spliterator(), isParallel);
    }
}

I think it's perfectly readable because you don't have to think about spliterators and booleans (isParallel).

Comments

5

A very simple work-around for this issue is to create a Streamable<T> interface extending Iterable<T> that holds a default <T> stream() method.

interface Streamable<T> extends Iterable<T> {
    default Stream<T> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
}

Now any of your Iterable<T>s can be trivially made streamable just by declaring them implements Streamable<T> instead of Iterable<T>.

Comments

0

If you happen to use Vavr(formerly known as Javaslang), this can be as easy as:

Iterable i = //...
Stream.ofAll(i);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.