Stream API的使用

1.例子一

/**
 * 1. Stream關注的是對數據的運算,與CPU打交道
 *    集合關注的是數據的存儲,與內存打交道
 *
 * 2.
 * ①Stream 自己不會存儲元素。
 * ②Stream 不會改變源對象。相反,他們會返回一個持有結果的新Stream。
 * ③Stream 操作是延遲執行的。這意味着他們會等到需要結果的時候才執行
 *
 * 3.Stream 執行流程
 * ① Stream的實例化
 * ② 一系列的中間操作(過濾、映射、...)
 * ③ 終止操作
 *
 * 4.說明:
 * 4.1 一箇中間操作鏈,對數據源的數據進行處理
 * 4.2 一旦執行終止操作,就執行中間操作鏈,併產生結果。之後,不會再被使用
 *
 *
 *  測試Stream的實例化
 *
 * @author shkstart
 * @create 2019 下午 4:25
 */
public class StreamAPITest {

    //創建 Stream方式一:通過集合
    @Test
    public void test1(){
        List<Employee> employees = EmployeeData.getEmployees();

//        default Stream<E> stream() : 返回一個順序流
        Stream<Employee> stream = employees.stream();

//        default Stream<E> parallelStream() : 返回一個並行流
        Stream<Employee> parallelStream = employees.parallelStream();

    }

    //創建 Stream方式二:通過數組
    @Test
    public void test2(){
        int[] arr = new int[]{1,2,3,4,5,6};
        //調用Arrays類的static <T> Stream<T> stream(T[] array): 返回一個流
        IntStream stream = Arrays.stream(arr);

        Employee e1 = new Employee(1001,"Tom");
        Employee e2 = new Employee(1002,"Jerry");
        Employee[] arr1 = new Employee[]{e1,e2};
        Stream<Employee> stream1 = Arrays.stream(arr1);

    }
    //創建 Stream方式三:通過Stream的of()
    @Test
    public void test3(){

        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

    }

    //創建 Stream方式四:創建無限流
    @Test
    public void test4(){

//      迭代
//      public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
        //遍歷前10個偶數
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);


//      生成
//      public static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章