Java中的IntStream noneMatch()及其示例

IntStream非匹配(IntPredicate谓词) 返回此流的元素是否与提供的谓词匹配。如果不是确定结果所必需的,它可能不会对所有元素的谓词求值。这是一个 短路终端操作。 如果终端操作在无限输入时可能在有限时间内终止,则终端操作为短路。 语法:

null
boolean noneMatch(IntPredicate predicate)

Where, IntPredicate represents a predicate (boolean-valued function)
of one int-valued argument and the function returns true if either
all elements of the stream match the provided predicate or 
the stream is empty, otherwise false.

注: 如果流为空,则返回true,并且不计算谓词。

例1: 函数的作用是:检查IntStream的元素是否不能被5整除。

// Java code for IntStream noneMatch
// (Predicate predicate) to check whether
// no element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream
IntStream stream = IntStream.of( 3 , 5 , 9 , 12 , 14 );
// Check if no element of stream
// is divisible by 5 using
// IntStream noneMatch(Predicate predicate)
boolean answer = stream.noneMatch(num -> num % 5 == 0 );
// Displaying the result
System.out.println(answer);
}
}


输出:

false

例2: 函数的作用是:检查连接两个IntStream后获得的IntStream中的元素是否小于2。

// Java code for IntStream noneMatch
// (Predicate predicate) to check whether
// no element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an IntStream after concatenating
// two IntStreams
IntStream stream = IntStream.concat(IntStream.of( 3 , 4 , 5 , 6 ),
IntStream.of( 7 , 8 , 9 , 10 ));
// Check if no element of stream
// is less than 2 using
// IntStream noneMatch(Predicate predicate)
boolean answer = stream.noneMatch(num -> num < 2 );
// Displaying the result
System.out.println(answer);
}
}


输出:

true

例3: 函数的作用是:显示流是否为空,然后返回true。

// Java code for IntStream noneMatch
// (Predicate predicate) to check whether
// no element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating an empty IntStream
IntStream stream = IntStream.empty();
// Using IntStream noneMatch() on empty stream
boolean answer = stream.noneMatch(num -> true );
// Displaying the result
System.out.println(answer);
}
}


输出:

true

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享