# Java8之Stream的实例化 **Repository Path**: fpfgitmy_admin/java8-stream-instance ## Basic Information - **Project Name**: Java8之Stream的实例化 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Stream的实例化 #### 方式一:通过集合 + `default Stream stream();` 返回一个顺序流 + `default Stream parallelStream();` 返回一个并行流 ``` @Test public void test1() { Collection list = new ArrayList(); // 顺序流 Stream stream = list.stream(); // 并行流 Stream integerStream = list.parallelStream(); } ``` #### 方式二:通过数组 + 根据传入的类型返回对应类型的流 ``` @Test public void test2() { int[] intArr = new int[]{1,2,23,4,5,6}; IntStream stream = Arrays.stream(intArr); } ``` #### 方式三:通过Stream的of() ``` @Test public void test3() { Stream stream = Stream.of(1, 2, 3, 4, 5); } ``` #### 方式四:创建无限流 ``` @Test public void test4() { // 迭代 // 遍历前10个偶数 // iterate(properties,function); 该方法参数1表示 起始值, 参数二表示对该起始值进行操作的方法 // 遍历前10个需要进行limit Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println); // 生成10个随机数 Stream.generate(Math::random).limit(10).forEach(System.out::println); } ```