Close

Java 8 Streams - LongStream.anyMatch Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.LongStream

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

Method:

boolean anyMatch(LongPredicate predicate)

This is a terminal-short-circuiting operation. It checks whether any elements of this LongStream match the provided LongPredicate. It will terminate (short-circuiting) with a result of 'true' when the very first element matches the provided predicate.

If the stream is empty then false is returned and the predicate is not evaluated.

Examples


package com.logicbig.example.longstream;

import java.util.stream.LongStream;

public class AnyMatchExample {

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

LongStream stream2 = LongStream.of(2, 7, 14);
boolean b2 = stream2.anyMatch(AnyMatchExample::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 AnyMatchExample2 {

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

Output

false




See Also