Question: In Java 8 we have the class [Stream<T>](http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html), which curiously have a method

```
Iterator<T> iterator()

```

So you would expect it to implement interface [Iterable<T>](http://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html), 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?

Answer: 
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](http://ideone.com/45QSHu))

(This uses a Java 8 functional interface cast.)

(This is covered in some of the comments above (e.g. [Aleksandr Dubinsky](https://stackoverflow.com/questions/20129762/why-does-streamt-not-implement-iterablet#comment30152645_20130475)), but I wanted to pull it out into an answer to make it more visible.)
If you don't mind using third party libraries [cyclops-react](https://github.com/aol/cyclops-react) defines a Stream that implements both Stream and Iterable and is replayable too (solving the problem [kennytm described](https://stackoverflow.com/a/20130131/1188377)).

```
 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]