Java Stream of toStream(final Iterable iterable)

Here you can find the source of toStream(final Iterable iterable)

Description

to Stream

License

Open Source License

Declaration

public static <U> Stream<U> toStream(final Iterable<U> iterable) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

import java.util.PrimitiveIterator;

import java.util.Spliterators;

import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {
    /**/*from  ww w.j a va 2  s  .com*/
     * This is a shortcut on {@link Spliterators#spliteratorUnknownSize(Iterator, int)} and
     * {@link StreamSupport#stream(Spliterator, boolean)}. There are more combinations of these 
     * methods. Have a look on these methods if you like a different behavior.
     * @param iterator
     * @see IteratorUtils#toStream(java.util.PrimitiveIterator.OfInt)
     * @see IteratorUtils#toStream(java.util.PrimitiveIterator.OfLong)
     * @see IteratorUtils#toStream(java.util.PrimitiveIterator.OfDouble)
     * @return
     */
    public static <U> Stream<U> toStream(final Iterator<U> iterator) {
        return StreamSupport.stream(() -> Spliterators.spliteratorUnknownSize(iterator, 0), 0, false);
    }

    public static <U> Stream<U> toStream(final Iterable<U> iterable) {
        return toStream(iterable.iterator());
    }

    /**
     * This is a shortcut on {@link Spliterators#spliteratorUnknownSize(PrimitiveIterator.OfInt, int)} and
     * {@link StreamSupport#stream(Spliterator, boolean)}. There are more combinations of these 
     * methods. Have a look on these methods if you like a different behavior.<br/>
     * Be aware that there are also static factory methods on {@link IntStream} that can be used instead of wrapping a 
     * PrimitiveIterator.OfInt. 
     * @see IntStream#range(int, int)
     * @see IntStream#rangeClosed(int, int)
     * @see IntStream#of(int...)
     * @see IntStream#of(int)
     * @param iterator
     * @return
     */
    public static <U> IntStream toStream(final PrimitiveIterator.OfInt iterator) {
        return StreamSupport.intStream(() -> Spliterators.spliteratorUnknownSize(iterator, 0), 0, false);
    }

    /**
     * This is a shortcut on {@link Spliterators#spliteratorUnknownSize(PrimitiveIterator.OfLong, int)} and
     * {@link StreamSupport#stream(Spliterator, boolean)}. There are more combinations of these 
     * methods. Have a look on these methods if you like a different behavior.<br/>
     * Be aware that there are also static factory methods on {@link LongStream} that can be used instead of wrapping a 
     * PrimitiveIterator.OfLong. 
     * @param iterator
     * @return
     */
    public static <U> LongStream toStream(final PrimitiveIterator.OfLong iterator) {
        return StreamSupport.longStream(() -> Spliterators.spliteratorUnknownSize(iterator, 0), 0, false);
    }

    /**
     * This is a shortcut on {@link Spliterators#spliteratorUnknownSize(Iterator, int)} and
     * {@link StreamSupport#stream(Spliterator, boolean)}. There are more combinations of these 
     * methods. Have a look on these methods if you like a different behavior.
     * @param iterator
     * @return
     */
    public static <U> DoubleStream toStream(final PrimitiveIterator.OfDouble iterator) {
        return StreamSupport.doubleStream(() -> Spliterators.spliteratorUnknownSize(iterator, 0), 0, false);
    }
}

Related

  1. streamToList(Stream stream)
  2. toParallelStream(Iterator iter)
  3. toStream(Collection collection)
  4. toStream(Enumeration e)
  5. toStream(final Iterable iterable)
  6. toStream(final T source)
  7. toStream(Iterable iterable)
  8. toStream(Iterator iter, boolean parallel)
  9. toStream(Optional value)