LongStream anyMatch(LongPredicate谓词)返回此流的任何元素是否与提供的谓词匹配。如果不一定要确定结果,则可能不会评估所有元素上的谓词。这是短路端子操作。如果出现无限输入时,端子操作可能会在有限时间内终止,则该端子操作会发生短路。
用法:
boolean anyMatch(LongPredicate predicate)
参数:
- LongPredicate : 一个long值参数的谓词(布尔值函数)。
返回值:如果流中的任何元素与提供的谓词匹配,则该函数返回true,否则返回false。
注意:如果流为空,则返回false,并且不对该谓词求值。
示例1:anyMatch()函数,用于检查列表中的任何元素是否满足给定条件。
// Java code for LongStream anyMatch
// (LongPredicate predicate) to check whether
// any element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L, 6L);
// LongStream anyMatch(LongPredicate predicate)
boolean answer = stream.anyMatch(num -> (num - 5) > 0);
// Displaying the result
System.out.println(answer);
}
}
输出:
true
示例2:anyMatch()函数,用于检查流中任何元素的平方根是否大于8。
// Java code for LongStream anyMatch
// (LongPredicate predicate) to check whether
// any element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an LongStream
LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);
// LongStream anyMatch(LongPredicate predicate)
boolean answer = stream.anyMatch(num -> Math.sqrt(num) > 8);
// Displaying the result
System.out.println(answer);
}
}
输出:
false
示例3:anyMatch()函数,以显示如果流为空,则返回false。
// Java code for LongStream anyMatch
// (LongPredicate predicate) to check whether
// any element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an empty LongStream
LongStream stream = LongStream.empty();
// LongStream anyMatch(LongPredicate predicate)
boolean answer = stream.anyMatch(num -> true);
// Displaying the result
System.out.println(answer);
}
}
输出:
false
相关用法
- Java DoubleStream anyMatch()用法及代码示例
- Java Stream anyMatch()用法及代码示例
- Java IntStream anyMatch()用法及代码示例
- Java LongStream min()用法及代码示例
- Java LongStream max()用法及代码示例
- Java LongStream count()用法及代码示例
- Java LongStream empty()用法及代码示例
- Java LongStream average()用法及代码示例
- Java LongStream mapToInt()用法及代码示例
- Java LongStream noneMatch()用法及代码示例
- Java LongStream allMatch()用法及代码示例
- Java LongStream peek()用法及代码示例
- Java LongStream distinct()用法及代码示例
- Java LongStream filter()用法及代码示例
- Java LongStream reduce(long identity, LongBinaryOperator op)用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream anyMatch() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。