Close

Java 8 Streams - IntStream.allMatch Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.IntStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.IntStreamIntStreamLogicBig

Method:

boolean allMatch(IntPredicate predicate)

This terminal-short-circuiting operation returns whether all elements match the provided predicate criteria. It returns 'false' on finding the first mismatch hence short-circuiting the operation.

If the stream is empty then this method returns true and the predicate is not evaluated.

Examples


package com.logicbig.example.intstream;

import java.util.stream.IntStream;

public class AllMatchExample {

public static void main(String... args) {
IntStream intStream = IntStream.of(1, 2, 3, 2, 5, 4);
boolean b = intStream.allMatch(AllMatchExample::greaterThanZero);
System.out.println(b);

IntStream intStream2 = IntStream.of(1, 2, 3, 2, 5, 4);
boolean b2 = intStream2.allMatch(AllMatchExample::evenNumber);
System.out.println(b2);
}

private static boolean evenNumber(int i) {
return i % 2 == 0;
}

private static boolean greaterThanZero(int i) {
return i > 0;
}
}

Output

true
false




For empty stream this method will always return true.

package com.logicbig.example.intstream;

import java.util.stream.IntStream;

public class AllMatchExample2 {

public static void main(String... args) {
IntStream intStream = IntStream.empty();
boolean b = intStream.allMatch(i -> false);
System.out.println(b);
}
}

Output

true




See Also