Why does Stream<T> not implement Iterable<T>?
JavaJava 8Java StreamIterableJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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)
.
Solution 2 - Java
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 );
Solution 3 - Java
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
.
Solution 4 - Java
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.)
Solution 5 - Java
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.
Solution 6 - Java
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);
}
Solution 7 - Java
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]
Solution 8 - Java
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.
Solution 9 - Java
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();
// ...
}