288
votes

In Java 8 we have the class Stream<T>, which curiously have a method

Iterator<T> iterator()

So you would expect it to implement interface Iterable<T>, which requires exactly this method, but that's not the case.

When I want to iterate over a Stream using a foreach loop, I have to do something like

public static Iterable<T> getIterable(Stream<T> s) {
    return new Iterable<T> {
        @Override
        public Iterator<T> iterator() {
            return s.iterator();
        }
    };
}

for (T element : getIterable(s)) { ... }

Am I missing something here?

9
not to mention that the other 2 methods of iterable (forEach and spliterator) are also in Streamnjzk2
this is needed to pass Stream to legacy APIs that expects IterableZhongYu
A good IDE (e.g. IntelliJ) will prompt you to simplify your code in getIterable() to return s::iterator;ZhongYu
You don't need a method at all. Where you have a Stream, and want an Iterable, just pass stream::iterator (or, if you prefer, () -> stream.iterator()), and you're done.Brian Goetz
Unfortunately I cannot write for (T element : stream::iterator), so I'd still prefer if Stream would also implement Iterable or a method toIterable().Thorsten

9 Answers

212
votes

People have already asked the same on the mailing list ☺. The main reason is Iterable also has a re-iterable semantic, while Stream is not.

I think the main reason is that Iterable implies reusability, whereas Stream is something that can only be used once — more like an Iterator.

If Stream extended Iterable then existing code might be surprised when it receives an Iterable that throws an Exception the second time they do for (element : iterable).

173
votes

To convert a Stream to an Iterable, you can do

Stream<X> stream = null;
Iterable<X> iterable = stream::iterator

To pass a Stream to a method that expects Iterable,

void foo(Iterable<X> iterable)

simply

foo(stream::iterator) 

however it probably looks funny; it might be better to be a little bit more explicit

foo( (Iterable<X>)stream::iterator );
10
votes

I would like to point out that StreamEx does implement Iterable (and Stream), as well as a host of other immensely awesome functionality missing from Stream.

10
votes

You can use a Stream in a for loop as follows:

Stream<T> stream = ...;

for (T x : (Iterable<T>) stream::iterator) {
    ...
}

(Run this snippet here)

(This uses a Java 8 functional interface cast.)

(This is covered in some of the comments above (e.g. Aleksandr Dubinsky), but I wanted to pull it out into an answer to make it more visible.)

7
votes

kennytm described why it's unsafe to treat a Stream as an Iterable, and Zhong Yu offered a workaround that permits using a Stream as in Iterable, albeit in an unsafe manner. It's possible to get the best of both worlds: a reusable Iterable from a Stream that meets all the guarantees made by the Iterable specification.

Note: SomeType is not a type parameter here--you need to replace it with a proper type (e.g., String) or resort to reflection

Stream<SomeType> stream = ...;
Iterable<SomeType> iterable = stream.collect(toList()):

There is one major disadvantage:

The benefits of lazy iteration will be lost. If you planned to immediately iterate over all values in the current thread, any overhead will be negligible. However, if you planned to iterate only partially or in a different thread, this immediate and complete iteration could have unintended consequences.

The big advantage, of course, is that you can reuse the Iterable, whereas (Iterable<SomeType>) stream::iterator would only permit a single use. If the receiving code will be iterating over the collection multiple times, this is not only necessary, but likely beneficial to performance.

3
votes

Stream does not implement Iterable. The general understanding of Iterable is anything that can be iterated upon, often again and again. Stream may not be replayable.

The only workaround that I can think of, where an iterable based on a stream is replayable too, is to re-create the stream. I am using a Supplier below to create a new instance of stream, everytime a new iterator is created.

    Supplier<Stream<Integer>> streamSupplier = () -> Stream.of(10);
    Iterable<Integer> iterable = () -> streamSupplier.get().iterator();
    for(int i : iterable) {
        System.out.println(i);
    }
    // Can iterate again
    for(int i : iterable) {
        System.out.println(i);
    }
2
votes

If you don't mind using third party libraries cyclops-react defines a Stream that implements both Stream and Iterable and is replayable too (solving the problem kennytm described).

 Stream<String> stream = ReactiveSeq.of("hello","world")
                                    .map(s->"prefix-"+s);

or :-

 Iterable<String> stream = ReactiveSeq.of("hello","world")
                                      .map(s->"prefix-"+s);

 stream.forEach(System.out::println);
 stream.forEach(System.out::println);

[Disclosure I am the lead developer of cyclops-react]

-1
votes

Not perfect, but will work:

iterable = stream.collect(Collectors.toList());

Not perfect because it will fetch all items from the stream and put them into that List, which is not exactly what Iterable and Stream are about. They are supposed to be lazy.

-1
votes

You can iterate over all files in a folder using Stream<Path> like this:

Path path = Paths.get("...");
Stream<Path> files = Files.list(path);

for (Iterator<Path> it = files.iterator(); it.hasNext(); )
{
    Object file = it.next();

    // ...
}