LongStream noneMatch() 方法在 Java 中
LongStream 類的 noneMatch() 方法在 Java 中返回這個流是否沒有元素匹配所提供的謂詞。
語法如下
boolean noneMatch(LongPredicate predicate)
這裡,引數 predicate 是要應用於此流的元素的無狀態謂詞。但是,語法中的 LongPredicate 表示一個長值自變數的謂詞(布林值函式)。
要在 Java 中使用 LongStream 類,請匯入以下包
import java.util.stream.LongStream;
該方法當流的任一元素都不匹配所提供的謂詞時返回 true,或者流為空時返回 true。以下是透過 noneMatch() 方法在 Java 中實現 LongStream 的示例
示例
import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(10L, 20L, 30L, 40L); boolean res = longStream.noneMatch(a -> a > 50); System.out.println("None of the element match the predicate? "+res); } }
返回 true,因為沒有元素匹配該謂詞
輸出
None of the element match the predicate? true
示例
import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(10L, 20L, 30L, 40L, 50, 60L); boolean res = longStream.noneMatch(a -> a < 30L); System.out.println("None of the element match the predicate? "+res); } }
返回 false,因為一個或多個元素匹配該謂詞
輸出
None of the element match the predicate? False
廣告