java8-新特性-04-stream的一些簡單應用

1.話不多說直接上代碼

import java.util.*;

public class Java8Test2 {


    public static void main(String[] args) {
        List<Integer> collection = 
        Arrays.asList(new Integer[]{14,5,43,89,64,112,55,58,55,61});

        /**
         * Count 計數
         * 計數是一個最終操作,返回Stream中元素的個數,返回值類型是long。
         */
        System.out.println(collection.parallelStream().count());

        /**
         * 求最大值,返回Option,通過Option.get()獲取值
         */
        System.out.println(collection.parallelStream().
        max((a,b)->{return a-b;}).get());

        /**
         * 求最小值,返回Option,通過Option.get()獲取值
         */
        System.out.println(collection.parallelStream().
        min((a,b)->{return a-b;}).get());

        /**
         * Filter 過濾方法
         */
        Long count =collection.stream().
                filter(num -> num!=null).
                filter(num -> num.intValue()>50).
                count();
        System.out.println("count:"+count);

        System.out.println("1===============");

        /**
         * distinct方法 去除重複
         */
        collection.stream().distinct().forEach(System.out::println);

        System.out.println("2===============");

        /**
         * Sort 排序
         *
         * 需要注意的是,排序只創建了一個排列好後的Stream,而不會影響原有的數據源,
         * 排序之後原數據stringCollection是不會被修改的:
         */
        collection.stream().sorted().forEach(System.out::println);

        /**
         * Map 映射
         * 對於Stream中包含的元素使用給定的轉換函數進行轉換操作,
         * 新生成的Stream只包含轉換生成的元素。
         * 這個方法有三個對於原始類型的變種方法,分別是:mapToInt,mapToLong和mapToDouble。
         * 這三個方法也比較好理解,比如mapToInt就是把原始Stream轉換成一個新的Stream,
         * 這個新生成的Stream中的元素都是int類型。
         * 之所以會有這樣三個變種方法,可以免除自動裝箱/拆箱的額外消耗;
         */
        System.out.println("3=============1");
        collection.stream().mapToLong(Long::valueOf).forEach(System.out::println);
        System.out.println("3=============2");
        Map<Integer, String> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            //putIfAbsent 不需要我們做額外的存在性檢查
            map.putIfAbsent(i, "val" + i);
        }
        map.forEach((id, val) -> System.out.println(val));
        System.out.println("3=============3");
        // Map 加了很多強大好用的方法
        System.out.println(map.getOrDefault(666, "not found"));  // not found


        /**
         * limit
         * 對一個Stream進行截斷操作,獲取其前N個元素,
         * 如果原Stream中包含的元素個數小於N,那就獲取其所有的元素
         */
        System.out.println("4=============");
        collection.stream().limit(5).forEach(System.out::println);

        /**
         * skip
         * 返回一個丟棄原Stream的前N個元素後剩下元素組成的新Stream,
         * 如果原Stream中包含的元素個數小於N,那麼返回空Stream;
         */
        System.out.println("5=============");
        collection.stream().skip(5).forEach(System.out::println);

        /**
         * Match 匹配
         * Stream提供了多種匹配操作,允許檢測指定的Predicate是否匹配整個Stream。
         * 所有的匹配操作都是最終操作,並返回一個boolean類型的值。
         */
        System.out.println("6=============");
        boolean anyStartsWithA = collection.stream().anyMatch((s) -> s>100);
        System.out.println(anyStartsWithA);//true  任意一個>100即可

        boolean allStartsWithA = collection.stream().allMatch((s) -> s>100);
        System.out.println(allStartsWithA);//false 全部>100方可

        boolean noneStartsWithZ = collection.stream().noneMatch((s) -> s>500);
        System.out.println(noneStartsWithZ);//true 沒有>500的

        /**
         * Reduce 規約
         * 這是一個最終操作,允許通過指定的函數來講stream中的多個元素規約爲一個元素,
         * 規越後的結果是通過Optional接口表示的
         */
        System.out.println("7=============");
        Optional<Integer> reduced = collection.stream().sorted().
        reduce((a,b) -> a + b);
        reduced.ifPresent(System.out::println);

        System.out.println("8=============");
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);

        /**
         *統計
         *
         * 另外,一些產生統計結果的收集器也非常有用。它們主要用於
         * int、double、long等基本類型上,
         * 它們可以用來產生類似如下的統計結果。
         */
        System.out.println("9=============");
        IntSummaryStatistics stats = collection.stream().
        mapToInt((x) -> x).summaryStatistics();
        System.out.println("列表中最大的數 : " + stats.getMax());
        System.out.println("列表中最小的數 : " + stats.getMin());
        System.out.println("所有數之和 : " + stats.getSum());
        System.out.println("平均數 : " + stats.getAverage());

    }
}

返回值:

10
112
5
count:7
1===============
14
5
43
89
64
112
55
58
61
2===============
5
14
43
55
55
58
61
64
89
112
3=============1
14
5
43
89
64
112
55
58
55
61
3=============2
val0
val1
val2
val3
val4
val5
val6
val7
val8
val9
3=============3
not found
4=============
14
5
43
89
64
5=============
112
55
58
55
61
6=============
true
false
true
7=============
556
8=============
-553262849
-1377477142
-1193048375
-436306697
-535213014
-1207581915
-1674081021
-705385961
-1102123833
1963385504
9=============
列表中最大的數 : 112
列表中最小的數 : 5
所有數之和 : 556
平均數 : 55.6

 

 

感謝:https://blog.csdn.net/cdw8131197/article/details/68553148

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