IntStream allMatch() 方法在 Java 中


Java 中 IntStream 類的 allMatch() 方法返回此流的所有元素是否都匹配提供的謂詞。

語法如下

boolean allMatch(IntPredicate predicate)

此處,predicate 引數是對此流的元素應用的無狀態謂詞。IntPredicate 表示一個 int 值引數的謂詞。

如果流的所有元素都匹配提供的謂詞,或者流為空,則 allMatch() 方法返回 true。

以下是使用 Java 中的 IntStream allMatch() 方法的示例

示例

 即時演示

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(55, 65, 70, 90, 100);
      boolean res = intStream.allMatch(a -> a > 50);
      System.out.println("Does all the elements of the stream matches the predicate?"+res);
   }
}

輸出

Does all the elements of the stream matches the predicate?true

示例

 即時演示

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 20, 25, 40, 50, 80);
      boolean res = intStream.allMatch(a -> a < 30);
      System.out.println("Do all the elements of the stream match the predicate? "+res);
   }
}

輸出

Do all the elements of the stream match the predicate? False

更新於: 30-07-2019

511 次瀏覽

開始你的 職業生涯

完成該課程以獲得認證

開始
廣告
© . All rights reserved.