Java Stream of stream(Iterable iterable)

Here you can find the source of stream(Iterable iterable)

Description

Converts an Iterable into a Stream.

License

Open Source License

Parameter

Parameter Description
iterable The iterable to stream.
T The type of the iterable

Return

Stream of the values returned by the iterable

Declaration

public static <T> Stream<T> stream(Iterable<T> iterable) 

Method Source Code


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

import java.util.*;

import java.util.stream.*;

public class Main {
    /**/*from  w  w  w  .  j  ava 2 s.c om*/
     * Converts an Optional value to a stream of 0..1 values
     * @param optional source optional value
     * @param <T> The type of the optional value
     * @return Stream of a single item of type T or an empty stream
     */
    public static <T> Stream<T> stream(Optional<T> optional) {
        return optional.map(Stream::of).orElseGet(Stream::empty);
    }

    /**
     * Converts an Iterable into a Stream.
     * @param iterable The iterable to stream.
     * @param <T> The type of the iterable
     * @return Stream of the values returned by the iterable
     */
    public static <T> Stream<T> stream(Iterable<T> iterable) {
        return StreamSupport.stream(iterable.spliterator(), false);
    }
}

Related

  1. stream(Collection collection)
  2. stream(Enumeration enumeration)
  3. stream(final Iterator iterator)
  4. stream(Iterable iterable)
  5. stream(Iterable input)
  6. stream(Iterable iterable)
  7. stream(Iterable it)
  8. stream(Iterator iterator)
  9. stream(Object values)