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
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
示例
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
广告