Java 中的 IntStream anyMatch() 方法


Java 中 IntStream 類中的 anyMatch() 方法返回該流的任何元素是否與提供的謂詞匹配。

語法如下

boolean anyMatch(IntPredicate predicate)

要使用 Java 中的 IntStream 類,匯入以下包

import java.util.stream.IntStream;

這裡,謂詞引數是應運用於該流的元素的不含狀態的謂詞。

建立 IntStream 並新增一些元素

IntStream intStream = IntStream.of(20, 40, 60, 80, 100);

現在,使用 anyMatch() 方法來為條件設定。如果任何元素與該條件匹配,則返回 TRUE

boolean res = intStream.anyMatch(a -> a < 50);

以下是如何在 Java 中實現 IntStream anyMatch() 方法的示例

示例

 即時演示

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(20, 40, 60, 80, 100);
      boolean res = intStream.anyMatch(a -> a < 50);
      System.out.println(res);
   }
}

它找到了小於 50 的值,所以返回 true

輸出

true

更新於: 30-Jul-2019

530 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.