当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LongStream noneMatch()用法及代码示例


LongStream noneMatch(LongPredicate谓词)返回此流中是否没有元素与提供的谓词匹配。如果不一定要确定结果,则可能不会评估所有元素上的谓词。这是短路端子操作。如果出现无限输入时,端子操作可能会在有限时间内终止,则该端子操作会发生短路。
用法:

boolean noneMatch(LongPredicate predicate) 

参数:

  1. LongPredicate : 一个long值参数的谓词(布尔值函数)。

返回值:如果流中的所有元素都与提供的谓词匹配,或者流为空,则该函数返回true,否则返回false。


注意:如果流为空,则返回true,并且不评估谓词。

示例1:noneMatch()函数,用于检查LongStream的元素是否可被5整除。

// Java code for LongStream noneMatch 
// (LongPredicate predicate) to check whether 
// no 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(3L, 5L, 9L, 12L, 14L); 
  
        // Check if no element of stream 
        // is divisible by 5 using 
        // LongStream noneMatch(LongPredicate predicate) 
        boolean answer = stream.noneMatch(num -> num % 5 == 0); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
输出:
false

示例2:noneMatch()函数,用于检查将两个LongStream串联后获得的LongStream中的元素是否不小于2。

// Java code for LongStream noneMatch 
// (LongPredicate predicate) to check whether 
// no 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 after concatenating 
        // two LongStreams 
        LongStream stream = LongStream.concat(LongStream.of(3L, 4L, 5L, 6L), 
                                              LongStream.of(7L, 8L, 9L, 10L)); 
  
        // Check if no element of stream 
        // is less than 2 using 
        // LongStream noneMatch(LongPredicate predicate) 
        boolean answer = stream.noneMatch(num -> num < 2); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
输出:
true

示例3:noneMatch()函数,以显示流是否为空,然后返回true。

// Java code for LongStream noneMatch 
// (LongPredicate predicate) to check whether 
// no 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(); 
  
        // Using LongStream noneMatch() on empty stream 
        boolean answer = stream.noneMatch(num -> true); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
输出:
true


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream noneMatch() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。