Java中的Stream allMatch()及其示例

流allMatch(谓词) 返回此流的所有元素是否与提供的谓词匹配。如果不是确定结果所必需的,它可能不会对所有元素的谓词求值。这是一个 短路终端操作。 如果终端操作在无限输入时可能在有限时间内终止,则终端操作为短路。 语法:

null
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,并且不计算谓词。一旦使用流完成任何函数,在重新初始化之前,不能再次使用它。

例1: 函数检查所有元素是否都可以被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: 函数检查字符串的长度是否大于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: 函数的作用是:检查所有字符串在第一个索引处是否都有大写字符。

// 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
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享