函數語言程式設計 - 終端方法



當在流上呼叫終端方法時,流的迭代開始,以及任何其他鏈式流。迭代結束後,返回終端方法的結果。終端方法不返回流,因此一旦在流上呼叫終端方法,其非終端方法或中間方法的鏈式呼叫就會停止/終止。

通常,終端方法返回單個值,並對流的每個元素呼叫。以下是流介面的一些重要的終端方法。每個終端函式都接受一個謂詞函式,啟動元素的迭代,並對每個元素應用謂詞。

  • anyMatch − 如果謂詞對任何元素返回 true,則返回 true。如果沒有任何元素匹配,則返回 false。

  • allMatch − 如果謂詞對任何元素返回 false,則返回 false。如果所有元素都匹配,則返回 true。

  • noneMatch − 如果沒有任何元素匹配,則返回 true,否則返回 false。

  • collect − 將每個元素儲存到傳入的集合中。

  • count − 返回透過中間方法的元素計數。

  • findAny − 返回包含任何元素的 Optional 例項,或返回空例項。

  • findFirst − 返回 Optional 例項中的第一個元素。對於空流,返回空例項。

  • forEach − 對每個元素應用消費者函式。用於列印流的所有元素。

  • min − 返回流中最小的元素。根據傳入的比較器謂詞比較元素。

  • max − 返回流中最大的元素。根據傳入的比較器謂詞比較元素。

  • reduce − 使用傳入的謂詞將所有元素歸約為單個元素。

  • toArray − 返回流元素的陣列。

示例 - 終端方法

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FunctionTester {    
   public static void main(String[] args) {
      List<String> stringList 
         = Arrays.asList("One", "Two", "Three", "Four", "Five", "One");       

      System.out.println("Example - anyMatch\n");
      //anyMatch - check if Two is present?
      System.out.println("Two is present: " 
         + stringList
         .stream()
         .anyMatch(s -> {return s.contains("Two");}));

      System.out.println("\nExample - allMatch\n");
      //allMatch - check if length of each string is greater than 2.
      System.out.println("Length > 2: " 
         + stringList
         .stream()
         .allMatch(s -> {return s.length() > 2;}));

      System.out.println("\nExample - noneMatch\n");
      //noneMatch - check if length of each string is greater than 6.
      System.out.println("Length > 6: " 
         + stringList
         .stream()
         .noneMatch(s -> {return s.length() > 6;}));

      System.out.println("\nExample - collect\n");
      System.out.println("List: " 
         + stringList
         .stream()
         .filter(s -> {return s.length() > 3;})
         .collect(Collectors.toList()));

      System.out.println("\nExample - count\n");
      System.out.println("Count: " 
         + stringList
         .stream()
         .filter(s -> {return s.length() > 3;})
         .count());

      System.out.println("\nExample - findAny\n");
      System.out.println("findAny: " 
         + stringList
         .stream()      
         .findAny().get());

      System.out.println("\nExample - findFirst\n");
      System.out.println("findFirst: " 
         + stringList
         .stream()      
         .findFirst().get());

      System.out.println("\nExample - forEach\n");
      stringList
         .stream()      
         .forEach(System.out::println);

      System.out.println("\nExample - min\n");
      System.out.println("min: " 
         + stringList
         .stream()      
         .min((s1, s2) -> { return s1.compareTo(s2);}));

      System.out.println("\nExample - max\n");
      System.out.println("min: " 
         + stringList
         .stream()      
         .max((s1, s2) -> { return s1.compareTo(s2);}));

      System.out.println("\nExample - reduce\n");
      System.out.println("reduced: " 
         + stringList
         .stream()      
         .reduce((s1, s2) -> { return s1 + ", "+ s2;})
         .get());
   }   
}

輸出

Example - anyMatch

Two is present: true

Example - allMatch

Length > 2: true

Example - noneMatch

Length > 6: true

Example - collect

List: [Three, Four, Five]

Example - count

Count: 3

Example - findAny

findAny: One

Example - findFirst

findFirst: One

Example - forEach

One
Two
Three
Four
Five
One

Example - min

min: Optional[Five]

Example - max

min: Optional[Two]

Example - reduce

reduced: One, Two, Three, Four, Five, One
廣告
© . All rights reserved.