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


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


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

boolean noneMatch(DoublePredicate predicate)

Where, DoublePredicate represents a predicate 
(boolean-valued function) of one double-valued argument.

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

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


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

// Java code for DoubleStream noneMatch 
// (DoublePredicate predicate) to check whether 
// no element of this stream match 
// the provided predicate. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream 
        DoubleStream stream = 
              DoubleStream.of(3.2, 5.0, 9.3, 12.4, 14.7); 
  
        // Check if no element of stream 
        // is divisible by 5 using 
        // DoubleStream noneMatch(DoublePredicate predicate) 
        boolean answer = 
             stream.noneMatch(num -> num % 5 == 0); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
输出:
false

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

// Java code for DoubleStream noneMatch 
// (DoublePredicate predicate) to check whether 
// no element of this stream match 
// the provided predicate. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream after 
        // concatenating two DoubleStreams 
        DoubleStream stream = DoubleStream.concat( 
            DoubleStream.of(3.3, 4.2, 5.1, 6.6), 
            DoubleStream.of(7.2, 8.3, 9.1, 10.5)); 
  
        // Check if no element of stream 
        // is less than 2 using 
        // DoubleStream noneMatch(DoublePredicate predicate) 
        boolean answer = stream.noneMatch(num -> num < 2); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
输出:
true

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

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


相关用法


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