Java8-04-01-笔记

创建Stream

1,Collection系列集合

通过Collection系列集合提供的方法stream()或者parallelStream()

default Stream<E> stream() :返回一个顺序流
default Stream<E> parallelStream() :返回一个并行流
package java.util;
//...

/*
 * The root interface in the <i>collection hierarchy</i>.  A collection
 * represents a group of objects, known as its <i>elements</i>.  Some
 * collections allow duplicate elements and others do not.  Some are ordered
 * and others unordered.  The JDK does not provide any <i>direct</i>
 * implementations of this interface: it provides implementations of more
 * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>.  This interface
 * is typically used to pass collections around and manipulate them where
 * maximum generality is desired.
 */
public interface Collection<E> extends Iterable<E> {
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
    default Stream<E> parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
    ...}
  • 应用案例
    List<String> list = new ArrayList<>();
    Stream<String> stream1 = list.stream();
        
    Set<String> set = new HashSet<>();
    Stream<String> stream2 = set.stream();
        
    Map<String,String> map = new HashMap<>();
    //map是不可以获取stream流的,除非对键或值的集合进行操作
    

2,Arrays静态方法

通过Arrays中的静态方法stream获取数组的流

public static <T> Stream<T> stream(T[] array)
//重载形式,能够处理对应基本类型的数组
public static IntStream stream(int[] array)
public static LongStream stream(long[] array)
public static DoubleStream stream(double[] array)
package java.util;
//...

/*
 * This class contains various methods for manipulating arrays (such as
 * sorting and searching). This class also contains a static factory
 * that allows arrays to be viewed as lists.
 */
public class Arrays {
	public static <T> Stream<T> stream(T[] array) {
        return stream(array, 0, array.length);
    }
    ...}
  • 应用案例
    Employee[] employees = new Employee[10];
    Stream<Employee> stream3 = Arrays.stream(employees);
    

3,Stream多值创建流

由值创建流,通过Stream类的静态方法of()创建一个流,可以接收任意数量的参数

public static<T> Stream<T> of(T... values)
package java.util.stream;
//...

public interface Stream<T> extends BaseStream<T, Stream<T>> {
	public static<T> Stream<T> of(T... values) {
        return Arrays.stream(values);
    }
    ...}
  • 应用案例
    Stream<String> stream4 = Stream.of("sun","stone","hahah");
    

4,Stream无限流

由函数创建流:创建无限流,使用静态方法Stream.iterate()和Stream.generate()创建无限流

迭代:public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
生成:public static<T> Stream<T> generate(Supplier<T> s)
package java.util.stream;
//...

public interface Stream<T> extends BaseStream<T, Stream<T>> {
	public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
       .../*
       @FunctionalInterface
		public interface UnaryOperator<T> extends Function<T, T> {
		
	     * Returns a unary operator that always returns its input argument.
	     *
	     * @param <T> the type of the input and output of the operator
	     * @return a unary operator that always returns its input argument
	
		    static <T> UnaryOperator<T> identity() {
		        return t -> t;
		    }
		}
       */
    }
    public static<T> Stream<T> generate(Supplier<T> s) {
        Objects.requireNonNull(s);
        return StreamSupport.stream(
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
    }
    ...}
  • 应用案例

    Stream<Integer> stream5 = Stream.iterate(0,(x)->x+2);
    stream5.limit(7).forEach(System.out::println);//换行打印 0 2 4 6 8 10 12
    
    Stream.generate(()->Math.random()).limit(5).forEach(System.out::println);//换行打印5个随机生成数
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值