Java Stream of stream(Iterable it)

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

Description

Create a stream from an iterable
   assertThat(StreamUtils.stream(Arrays.asList(1,2,3)) .collect(Collectors.toList()), equalTo(Arrays.asList(1,2,3))); 

License

Apache License

Parameter

Parameter Description
it Iterable to convert to a Stream

Return

Stream from iterable

Declaration

public static <U> Stream<U> stream(Iterable<U> it) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

import java.util.Map;

import java.util.Spliterator;
import java.util.Spliterators;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {
    /**/*from   w w w  .  j a  va2 s .  c  o m*/
     * Create a stream from an iterable
     * <pre>
     * {@code 
     *    assertThat(StreamUtils.stream(Arrays.asList(1,2,3))
     *                         .collect(Collectors.toList()),
     *                            equalTo(Arrays.asList(1,2,3)));
        
     * 
     * }
     * </pre>
     * @param it Iterable to convert to a Stream
     * @return Stream from iterable
     */
    public static <U> Stream<U> stream(Iterable<U> it) {
        return StreamSupport.stream(it.spliterator(), false);
    }

    public static <U> Stream<U> stream(Spliterator<U> it) {
        return StreamSupport.stream(it, false);
    }

    /**
     * Create a stream from an iterator
     * <pre>
     * {@code 
     *    assertThat(StreamUtils.stream(Arrays.asList(1,2,3).iterator())   
     *                      .collect(Collectors.toList()),
     *                         equalTo(Arrays.asList(1,2,3)));
        
     * }
     * </pre>
     * @param it Iterator to convert to a Stream
     * @return Stream from iterator
     */
    public static <U> Stream<U> stream(Iterator<U> it) {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
    }

    /**
     * Create a stream from a map
     * <pre>
     * {@code 
     *    Map<String,String> map = new HashMap<>();
       map.put("hello","world");
       assertThat(StreamUtils.stream(map).collect(Collectors.toList()),equalTo(Arrays.asList(new AbstractMap.SimpleEntry("hello","world"))));
        
     * }</pre>
     * 
     * 
     * @param it Iterator to convert to a Stream
     * @return Stream from a map
     */
    public final static <K, V> Stream<Map.Entry<K, V>> stream(Map<K, V> it) {
        return it.entrySet().stream();
    }
}

Related

  1. stream(final Iterator iterator)
  2. stream(Iterable iterable)
  3. stream(Iterable input)
  4. stream(Iterable iterable)
  5. stream(Iterable iterable)
  6. stream(Iterator iterator)
  7. stream(Object values)
  8. streamEquals(Stream a, Stream b)
  9. streamInt(int max)