流allMatch(Predicate謂詞)返回此流的所有元素是否與提供的謂詞匹配。如果不一定要確定結果,則可能不會評估所有元素上的謂詞。這是短路端子操作。如果出現無限輸入時,端子操作可能會在有限時間內終止,則該端子操作會發生短路。句法:
boolean allMatch(Predicate<? super T> predicate) Where, T is the type of the input to the predicate and the function returns true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false.
注意:如果流為空,則返回true,並且不評估謂詞。一旦使用流完成任何函數,它就不能再次使用,除非它是re-initialize。
範例1:allMatch()函數來檢查是否所有元素都可以被3整除。
// Java code for Stream allMatch
// (Predicate predicate) to check whether
// all elements of this stream match
// the provided predicate.
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args) {
// Creating a list of Integers
List<Integer> list = Arrays.asList(3, 4, 6, 12, 20);
// Check if all elements of stream
// are divisible by 3 or not using
// Stream allMatch(Predicate predicate)
boolean answer = list.stream().allMatch(n-> n % 3 ==0);
// Displaying the result
System.out.println(answer);
}
}
輸出:
false
範例2:allMatch()函數,用於檢查字符串的長度是否大於2。
// Java code for Stream allMatch
// (Predicate predicate) to check whether
// all elements of this stream match
// the provided predicate.
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args) {
// Creating a Stream of Strings
Stream<String> stream = Stream.of("Geeks", "for",
"GeeksQuiz", "GeeksforGeeks");
// Check if all elements of stream
// have length greater than 2 using
// Stream allMatch(Predicate predicate)
boolean answer = stream.allMatch(str -> str.length() > 2);
// Displaying the result
System.out.println(answer);
}
}
輸出:
true
範例3:allMatch()函數檢查所有字符串在第一個索引處是否都具有UpperCase字符。
// Java code for Stream allMatch
// (Predicate predicate) to check whether
// all elements of this stream match
// the provided predicate.
import java.util.stream.Stream;
class GFG {
// Driver code
public static void main(String[] args) {
// Creating a Stream of Strings
Stream<String> stream = Stream.of("Geeks", "for",
"GeeksQuiz", "GeeksforGeeks");
// Check if Character at 1st index is
// UpperCase in all strings or not using
// Stream allMatch(Predicate predicate)
boolean answer = stream.allMatch(str-> Character
.isUpperCase(str.charAt(1)));
// Displaying the result
System.out.println(answer);
}
}
輸出:
false
範例4:使用同一流完成多項函數
// In case we want multiple function to be done.
import java.util.stream.IntStream;
public class MultipleStreamFunction {
public static void main(String[] args) {
final String sample = "Om Sarve Bhavantu Sukhinah";
// converting to Ascii
IntStream intstreams = sample.chars();
// All match to check if all Ascii value greater then 100
boolean answer = intstreams.allMatch(c -> c > 100);
System.out.println(answer);
// Need to initialize the stream again
// to avoid runtime exception
intstreams = sample.chars();
// All match to check if all Ascii value greater then 31
answer = intstreams.allMatch(c -> c > 31);
System.out.println(answer);
}
}
輸出:
false true
相關用法
- Java LongStream allMatch()用法及代碼示例
- Java DoubleStream allMatch()用法及代碼示例
- Java IntStream allMatch()用法及代碼示例
- Java Stream map()用法及代碼示例
- Java Stream.reduce()用法及代碼示例
- Java Stream min()用法及代碼示例
- Java Stream flatMapToInt()用法及代碼示例
- Java Stream flatMapToLong()用法及代碼示例
- Java Stream flatMapToDouble()用法及代碼示例
- Java Stream flatMap()用法及代碼示例
- Java Stream findFirst()用法及代碼示例
- Java Stream findAny()用法及代碼示例
- Java Stream peek()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Stream allMatch() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。