當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。