Close

Java 8 Streams - LongStream.allMatch Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.LongStream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.LongStreamLongStreamLogicBig

Method:

boolean allMatch(LongPredicate 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.longstream;

import java.util.stream.LongStream;

public class AllMatchExample {

public static void main(String... args) {
LongStream stream = LongStream.of(3, 6, 9);
boolean b = stream.allMatch(AllMatchExample::odd);
System.out.println(b);

LongStream stream2 = LongStream.of(3, 6, 14);
boolean b2 = stream2.allMatch(AllMatchExample::odd);
System.out.println(b2);
}

private static boolean odd(long lg) {
return lg % 3 == 0;
}
}

Output

true
false




package com.logicbig.example.longstream;

import java.util.stream.LongStream;

public class AllMatchExample2 {

public static void main(String... args) {
LongStream stream = LongStream.empty();
boolean b = stream.allMatch(lg -> false);
System.out.println(b);
}
}

Output

true




See Also