jdk 1.8 新特性 stream

 /**
         * .distinct()
         * .limit(2)
         */
        
        //初始化
        List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1);

        //遍歷
        integerList.forEach( item ->{
        });

        // map映射配個元素對應的結果
        List<Integer> squaresList = integerList.stream().map(i -> i*i).distinct().limit(2).collect(Collectors.toList());
        System.out.println("map映射配個元素對應的結果"+squaresList);



//        //過濾,收集所有偶數
        List<Integer> collecgt = integerList.stream()
                .filter(item -> item %2 ==1).distinct().collect(Collectors.toList());


        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filtered = strings.stream()
                .filter(string -> !string.isEmpty()).collect(Collectors.toList());

//        //計算總和
        int sum = integerList.stream()
              .mapToInt(Integer::intValue).sum();
        int sum1 = integerList.stream()
                .mapToInt(i->i).reduce(0,Integer :: sum); //帶初始值

        System.out.println("總和"+sum1);

        OptionalInt reduce1 = integerList
                .stream()
                .mapToInt(Integer::intValue)
                .reduce(Integer::sum);//不帶初始值

        System.out.println("計算總和:" + reduce1);

        Integer reduce2 = integerList
                .stream()
                .reduce(0, (a, b) -> a + b);

        System.out.println("計算總和:" + reduce2);

        //計算數量
        long count = integerList.stream().count();
        System.out.println("計算數量" + count);

        Optional<Integer> collect8 = integerList.stream().collect(Collectors.maxBy((x1, x2) -> x1 - x2));
        Optional<Integer> collect9 = integerList.stream().collect(Collectors.maxBy(Comparator.comparing(Integer::intValue)));
        Optional<Integer> collect10 = integerList.stream().collect(Collectors.minBy(Comparator.comparing(Integer::intValue)));
        if (collect8.isPresent()) System.out.println("求最大值:" + collect8.get());
        if (collect9.isPresent()) System.out.println("求最大值:" + collect9.get());
        if (collect10.isPresent()) System.out.println("求最小值:" + collect10.get());

//
//
//        //求平均值
//        Double collect11 = integerList.stream().collect(Collectors.averagingInt(Integer::intValue));
//        System.out.println("求平均值:" + collect11);
//
//
        //一次性得到元素個數、總和、均值、最大值、最小值
        IntSummaryStatistics collect12 = integerList.stream().collect(Collectors.summarizingInt(Integer::intValue));

        System.out.println("一次性得到元素個數、總和、均值、最大值、最小值:" + collect12);

        //分組
        Map<Integer, List<Integer>> collect15 = integerList.stream().collect(
                Collectors.groupingBy(Integer::intValue)

        );

        Map<Integer,List<Integer>> collec = integerList.stream().collect(Collectors.groupingBy(i -> i));
        System.out.println("分組:" + collect15);
        System.out.println("分組2:" + collec);

        Map<Integer, Long> collect14 = integerList.stream().collect(
                Collectors.groupingBy(Integer::intValue, Collectors.counting())
        );
        System.out.println("可以有多級分組:" + collect14);

        //分區可以看做是分組的一種特殊情況,在分區中key只有兩種情況:true或false
        Map<Boolean, List<Integer>> collect16 = integerList.stream().collect(Collectors.partitioningBy(x -> x >= 7));

        System.out.println("分區可以看做是分組的一種特殊情況,在分區中key只有兩種情況:true或false:" + collect16);
//
//        //去重
//        List<Integer> collect1 = integerList
//                .stream()
//                .distinct()
//                .collect(Collectors.toList());
//        System.out.println("去重:" + collect1);
//
//
//        //limit返回包含前n個元素的流
//        List<Integer> collect2 = integerList
//                .stream()
//                .filter(integer -> integer % 2 == 0).limit(2)
//                .collect(Collectors.toList());
//        System.out.println("limit:" + collect2);


        //排序,倒序排序
        List<Integer> collect3 = integerList.
                stream()
                .sorted((s1, s2) -> s2 - s1)
                .collect(Collectors.toList());
        System.out.println("排序:" + collect3);

        //跳過前n個元素
        List<Integer> collect4 = integerList
                .stream()
                .filter(integer -> integer % 2 == 1).skip(2)
                .collect(Collectors.toList());
        System.out.println("skip:" + collect4);


        String[] strs = {"java8", "is", "easy", "to", "use"};

        //字符串拼接
        String collect13 = Arrays.stream(strs).collect(Collectors.joining());

        System.out.println("字符串拼接:" + collect13);


//        List<String[]> collect5 = Arrays.stream(strs)
//                .map(s -> s.split(","))//將字符串映射成字符數組
//                .collect(Collectors.toList());
//        System.out.println("將字符串映射成字符數組:" + collect5);

        //flatMap是將一個流中的每個值都轉成一個個流,然後再將這些流扁平化成爲一個流
        List<String> collect7 = Arrays.stream(strs)
                .map(s -> s.split(""))//每個字符串映射成string[]
                .flatMap(Arrays::stream)//flatMap將由map映射得到的Stream<String[]>,轉換成由各個字符串數組映射成的流Stream<String>
                .collect(Collectors.toList());
        System.out.println("flatMap是將一個流中的每個值都轉成一個個流,然後再將這些流扁平化成爲一個流:" + collect7);


        //多個字符串將各個字符拆開,去重
        List<String> collect6 = Arrays.stream(strs)
                .map(s -> s.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());

        System.out.println("多個字符串將各個字符拆開,去重:" + collect6);


        //allMatch,檢測是否全部都滿足指定的參數行爲
        boolean b = integerList.stream().allMatch(integer -> integer > 5);
        System.out.println("allMatch,檢測是否全部都滿足指定的參數行爲:" + b);

        //anyMatch,檢測是否存在一個或多個滿足指定的參數行爲
        boolean any = integerList.stream().anyMatch(integer -> integer > 5);
        System.out.println("anyMatch,檢測是否存在一個或多個滿足指定的參數行爲:" + any);

        //nonMatch 檢測是否不存在滿足指定行爲的元素
        boolean non = integerList.stream().noneMatch(integer -> integer > 5);
        System.out.println("nonMatch 檢測是否不存在滿足指定行爲的元素:" + non);

        //用於返回滿足條件的第一個元素

        Optional<Integer> first = integerList.stream().filter(integer -> integer > 6).findFirst();
        if (first.isPresent()) System.out.println("用於返回滿足條件的第一個元素:" + first.get());

        //findAny相對於findFirst的區別在於,findAny不一定返回第一個,而是返回任意一個
        //實際上對於順序流式處理而言,findFirst和findAny返回的結果是一樣的,
        // 至於爲什麼會這樣設計,當我們啓用並行流式處理的時候,查找第一個元素往往會有很多限制,如果不是特別需求,
        // 在並行流式處理中使用findAny的性能要比findFirst好。
        Optional<Integer> any1 = integerList.stream().filter(integer -> integer > 1).distinct().findAny();
        if (first.isPresent()) System.out.println("findAny不一定返回第一個,而是返回任意一個:" + any1.get());
        // 啓動並行流式處理雖然簡單,只需要將stream()替換成parallelStream()即可,
        // 但既然是並行,就會涉及到多線程安全問題,所以在啓用之前要先確認並行是否值得
        // (並行的效率不一定高於順序執行),另外就是要保證線程安全。此兩項無法保證,
        // 那麼並行毫無意義,畢竟結果比速度更加重
        Optional<Integer> any2 = integerList.parallelStream().filter(integer -> integer > 1).distinct().findAny();
        if(any2.isPresent())System.out.println("並行流式處理:"+any2.get());


    }

 

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