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: 
[kennytm described](https://stackoverflow.com/a/20130131/1188377) why it's unsafe to treat a `Stream` as an `Iterable`, and [Zhong Yu offered a workaround](https://stackoverflow.com/a/20130475/1188377) 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.
`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);
    }

```