流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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。