JAVA之Lamdba表達式使用摘要

1. of (of方法其生成的Stream是有限長度的,Stream的長度爲其內的元素個數)
    Stream<Integer> integerStream = Stream.of(1, 2, 3);
    Stream<String> stringStream = Stream.of("A");

2. concat(concat方法將兩個Stream連接在一起)
    Stream.concat(Stream.of(1, 2, 3), Stream.of(4, 5))
       .forEach(integer -> System.out.print(integer + "  "));

3. distinct (distinct方法以達到去除掉原Stream中重複的元素,生成的新Stream中沒有沒有重複的元素)
    Stream.of(1,2,3,1,2,3)
        .distinct()
        .forEach(System.out::println); // 打印結果:1,2,3

4. filter (filter方法對原Stream按照指定條件過濾,在新建的Stream中,只包含滿足條件的元素,將不滿足條件的元素過濾掉)
    Stream.of(1, 2, 3, 4, 5)
        .filter(item -> item > 3)
        .forEach(System.out::println);// 打印結果:4,5

5. map (map方法將對於Stream中包含的元素使用給定的轉換函數進行轉換操作)
map方法將對於Stream中包含的元素使用給定的轉換函數進行轉換操作,新生成的Stream只包含轉換生成的元素。爲了提高處理效率,官方已封裝好了,三種變形:mapToDouble,mapToInt,mapToLong。其實很好理解,如果想將原Stream中的數據類型,轉換爲double,int或者是long是可以調用相對應的方法。
Stream.of("a", "b", "hello")
        .map(item-> item.toUpperCase())
        .forEach(System.out::println);
        // 打印結果
        // A, B, HELLO

6. flatMap(flatMap方法與map方法類似,都是將原Stream中的每一個元素通過轉換函數轉換)
    flatMap方法與map方法類似,都是將原Stream中的每一個元素通過轉換函數轉換,不同的是,該換轉函數的對象是一個Stream,也不會再創建一個新的Stream,而是將原Stream的元素取代爲轉換的Stream。如果轉換函數生產的Stream爲null,應由空Stream取代。    flatMap有三個對於原始類型的變種方法,分別是:flatMapToInt,flatMapToLong和flatMapToDouble。
    Stream.of(1, 2, 3)
    .flatMap(integer -> Stream.of(integer * 10))
    .forEach(System.out::println);
    // 打印結果
    // 10,20,30
    傳給flatMap中的表達式接受了一個Integer類型的參數,通過轉換函數,將原元素乘以10後,生成一個只有該元素的流,該流取代原流中的元素。

7. peek (peek方法生成一個包含原Stream的所有元素的新Stream)
peek方法生成一個包含原Stream的所有元素的新Stream,同時會提供一個消費函數(Consumer實例),新Stream每個元素被消費的時候都會執行給定的消費函數,並且消費函數優先執行
    Stream.of(1, 2, 3, 4, 5)
        .peek(integer -> System.out.println("accept:" + integer))
        .forEach(System.out::println);
// 打印結果
// accept:1
//  1
//  accept:2
//  2
//  accept:3
//  3
//  accept:4
//  4
//  accept:5
//  5

8. skip (skip方法將過濾掉原Stream中的前N個元素,返回剩下的元素所組成的新Stream)
skip方法將過濾掉原Stream中的前N個元素,返回剩下的元素所組成的新Stream。如果原Stream的元素個數大於N,將返回原Stream的後(原Stream長度-N)個元素所組成的新Stream;如果原Stream的元素個數小於或等於N,將返回一個空Stream。
示例:
Stream.of(1, 2, 3,4,5)
.skip(2)
.forEach(System.out::println);
// 打印結果
// 3,4,5

9. limit (limit方法將截取原Stream)
limit方法將截取原Stream,截取後Stream的最大長度不能超過指定值N。如果原Stream的元素個數大於N,將截取原Stream的前N個元素;如果原Stream的元素個數小於或等於N,將截取原Stream中的所有元素。
Stream.of(1, 2, 3,4,5)
        .limit(2)
        .forEach(System.out::println);
        // 打印結果
        // 1,2
傳入limit的值爲2,也就是說被截取後的Stream的最大長度爲2,又由於原Stream中有5個元素,所以將截取原Stream中的前2個元素,生成一個新的Stream。
分頁示例:Stream.of(1,2,3,4,5,6,7,8,9).skip(3).limit(3);

10. sorted (sorted方法將對原Stream進行排序)
sorted方法將對原Stream進行排序,返回一個有序列的新Stream。sorterd有兩種變體sorted(),sorted(Comparator),前者將默認使用Object.equals(Object)進行排序,而後者接受一個自定義排序規則函數(Comparator),可按照意願排序。
Stream.of(5, 4, 3, 2, 1)
        .sorted()
        .forEach(System.out::println);
        // 打印結果
        // 1,2,3,4,5

Stream.of(1, 2, 3, 4, 5)
        .sorted(Comparator.reverseOrder())
        .forEach(System.out::println);
        // 打印結果
        // 5, 4, 3, 2, 1

Comparator使用:https://www.cnblogs.com/mrhgw/p/9560974.html

11. max/min (max/min方法根據指定的Comparator,返回一個Optional)   
  Optional<Integer> max = Stream.of( 2,1, 3, 5, 4)
                .max((o1, o2) -> o1 - o2);
        System.out.println("max:" + max.get()); //打印結果 max:5

        Optional<Integer> min = Stream.of( 2,1, 3, 5, 4)
                .min((o1, o2) -> o1 - o2);
        System.out.println("min:" + min.get()); //打印結果 min:1
    剛纔在max方法中,我們找的是Stream中的最小值,在min中我們找的是Stream中的最大值,不管是最大值還是最小值起決定作用的是Comparator,它決定了元素比較大小的原則。

12. allMatch (allMatch操作用於判斷Stream中的元素是否全部滿足指定條件)
allMatch操作用於判斷Stream中的元素是否全部滿足指定條件。如果全部滿足條件返回true,否則返回false。

boolean allMatch = Stream.of(1, 2, 3, 4)
    .allMatch(integer -> integer > 0);
System.out.println("allMatch: " + allMatch); // 打印結果:allMatch: true


13. anyMatch (anyMatch操作用於判斷Stream中的是否有滿足指定條件的元素)
anyMatch操作用於判斷Stream中的是否有滿足指定條件的元素。如果最少有一個滿足條件返回true,否則返回false。
boolean anyMatch = Stream.of(1, 2, 3, 4)
    .anyMatch(integer -> integer > 3);
System.out.println("anyMatch: " + anyMatch); // 打印結果:anyMatch: true


14. findFirst (indFirst操作用於獲取含有Stream中的第一個元素的Optional)
    findFirst操作用於獲取含有Stream中的第一個元素的Optional,如果Stream爲空,則返回一個空的Optional。若Stream並未排序,可能返回含有Stream中任意元素的Optional。
    Optional<Integer> any = Stream.of(1, 2, 3, 4).findFirst();

    Integer[] xs = new Integer[0];
    Integer x = Arrays.stream(xs).findFirst().orElse(null);
    System.out.println(x);  //輸出null

15. noneMatch (noneMatch方法將判斷Stream中的所有元素是否滿足指定的條件)
noneMatch方法將判斷Stream中的所有元素是否滿足指定的條件,如果所有元素都不滿足條件,返回true;否則,返回false.

    boolean noneMatch = Stream.of(1, 2, 3, 4, 5)
        .noneMatch(integer -> integer > 10);
    System.out.println("noneMatch:" + noneMatch); // 打印結果 noneMatch:true

    boolean noneMatch_ = Stream.of(1, 2, 3, 4, 5)
            .noneMatch(integer -> integer < 3);
    System.out.println("noneMatch_:" + noneMatch_); // 打印結果 noneMatch_:false

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