Convert Iterable to Stream using Java 8 JDK
Asked Answered
A

8

514

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?

Adscription answered 29/5, 2014 at 11:16 Comment(6)
If you can iterate, why not simly use a loop to check its condition or value or what so ever?Selfexistent
@AfzaalAhmadZeeshan because streams are much betterBlythebm
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"Adscription
Seems weird that myIterable.stream() does not exist!Procrustean
In 2018 we also have Stream.of(iterable)Stoker
@Guillaume: Yes, but Stream.of(iterable) produces Stream<Iterable<Object>>.Erl
C
701

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.

Consuelaconsuelo answered 29/5, 2014 at 15:15 Comment(7)
A static method on Stream would have been nice, e.g. Stream.ofIterable(iterable).Cangue
@Cangue 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."Consuelaconsuelo
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().Christman
@Christman See #23114515.Consuelaconsuelo
@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.Porphyrin
Based on the explanations given so far, I would expect to have Stream.ofIterable(Iterable<T> iterable, boolean parallel), and Stream.ofIterable(Iterable<T> iterable), which defaults to a non-parallel stream of items. More generally speaking, StreamSupport helpers should be in the Stream class.Zachariah
@StéphaneAppercel Please read the comment above between Brian and Robin.Lavena
W
97

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

Streams.stream(iterable)
Woorali answered 16/1, 2017 at 9:11 Comment(2)
Or, if you are stuck at an older version, use Lists.newArrayList(Iterable).Wist
Current Guava implementation is not worse than accepted answer: github.com/google/guava/blob/master/guava/src/com/google/common/…Poppas
T
24

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
    );
}
Tinsley answered 29/5, 2014 at 11:26 Comment(3)
You have to write this function once and then just call it. Why does a call to stream(...) clutter your code?Calumny
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.Adscription
static import said function. short and elegant. (though not necessarily transparent)Interstice
H
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.

Hellgrammite answered 5/11, 2015 at 16:18 Comment(1)
Wow there is a whole JOO* family. I was only familiar with JOOQ. Thanks!Epigraphy
P
8

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
  );
}
Plebeian answered 3/11, 2017 at 12:2 Comment(0)
I
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).

Indite answered 21/4, 2016 at 10:31 Comment(0)
J
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>.

Junejuneau answered 10/4, 2017 at 11:31 Comment(0)
M
0

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

Iterable i = //...
Stream.ofAll(i);
Marilee answered 25/7, 2017 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.