public <R> Stream<R> flatMap(final Func1<? super T, ? extends Iterable<? extends R>> func) {
        return new Stream<R>() {
            @Override
            public Iterator<R> iterator() {
                return new ReadOnlyIterator<R>() {

                    Iterator<T> iterator = Stream.this.iterator();
                    Iterator<? extends R> next;

                    @Override
                    public boolean hasNext() {
                        while ((next == null || !next.hasNext()) && iterator.hasNext()) {
                            next = func.call(iterator.next()).iterator();
                        }

                        return next != null && next.hasNext();
                    }

                    @Override
                    public R next() {
                        return next.next();
                    }
                };
            }
        };
    }
public < R > Stream < R > flatMap ( final Func1 < ? super T , ? extends Iterable < ? extends R > > func ) { return new Stream < R > ( ) { @ Override public Iterator < R > iterator ( ) { return new ReadOnlyIterator < R > ( ) { Iterator < T > iterator = Stream . this . iterator ( ) ; Iterator < ? extends R > next ; @ Override public boolean hasNext ( ) { while ( ( next == null || ! next . hasNext ( ) ) && iterator . hasNext ( ) ) { next = func . call ( iterator . next ( ) ) . iterator ( ) ; } return next != null && next . hasNext ( ) ; } @ Override public R next ( ) { return next . next ( ) ; } } ; } } ; }
Returns a new stream that contains items that has been returned by a given function for each item in the current stream.
The difference from {@link #map(Func1)} is that a given function can return more than one item for
each item of the current list.

@param func a function that takes an item of the current stream and returns a stream of values for the new stream.
@param <R>  a type of items new stream returns.
@return a new stream that contains items that has been returned by a given function for each item in the current stream.