Java8新特性:创建Stream流


====================================

有四种创建集合的方式

  1. 可以通过 Collection 系列集合提供的 stream() 或者  parallelStream ()
  2. 可以通过 Collection 系列集合提供的 stream() 或者  parallelStream()
  3. 通过Stream的静态方法 of()
  4. 创建无限流     迭代  生成
public static void main(String[] args) {
    //1.可以通过 Collection 系列集合提供的 stream() 或者  parallelStream()
    List list = new ArrayList<>();
    Stream stream11 = list.stream();
    Stream stream12 = list.parallelStream();

    //2.可以通过Arrays中的静态方法 stream()获取一个数组流
    Employee[] employees = new Employee[10];
    Stream stream2 = Arrays.stream(employees);

    //3.通过Stream的静态方法 of()
    Stream stream31 = Stream.of("aa", "bb", "cc");
    Stream stream32 = Stream.of(employees);

    //====================================================
    //4.创建无限流
    //迭代  第一个参数是初始值,第二个参数是一个函数式接口
    Stream stream41 = Stream.iterate(0, (x) -> x + 2);

    stream41.limit(5).forEach(System.out::println);

    //生成
    Stream stream42 = Stream.generate(() -> Math.random());
    stream42.limit(5).forEach(System.out::println);

}