Here you can find the source of stream(Iterable it)
assertThat(StreamUtils.stream(Arrays.asList(1,2,3)) .collect(Collectors.toList()), equalTo(Arrays.asList(1,2,3)));
Parameter | Description |
---|---|
it | Iterable to convert to a Stream |
public static <U> Stream<U> stream(Iterable<U> it)
//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(); } }