Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaStream allMatch() in Java with examples

Stream allMatch() in Java with examples

Stream allMatch(Predicate predicate) returns whether all elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determining the result. This is a short-circuiting terminal operation. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time. Syntax :

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.

Note : If the stream is empty then true is returned and the predicate is not evaluated. Once any function is done using the stream it can’t be used again until it is re-initialize. Example 1 : allMatch() function to check whether all elements are divisible by 3. 

Java




// 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);
}
}


Output :

false

Example 2 : allMatch() function to check whether strings have length greater than 2. 

Java




// 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", "Lazyroar");
         
    // 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);
}
}


Output :

true

Example 3 : allMatch() function to check whether all strings have UpperCase character at 1st index. 

Java




// 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", "Lazyroar");
         
    // 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);
}
}


Output :

false

Example 4 : Multiple function done using same stream 

Java




// 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 than 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 than 31
 
        answer = intstreams.allMatch(c -> c > 31);
        System.out.println(answer);
 
    }
}


Output :

false
true

RELATED ARTICLES

Most Popular

Recent Comments