Something needs to be clarified: a boolean not a Predicate . boolean is a noun but Predicate is a predicate . so your Predicate looks something like this:
IntPredicate isEven = n -> n % 2 == 0; boolean numbersAreEven = numbers.stream().allMatch(isEven);
IF you need to invert Predicate , you can use Predicate # negate , for example:
IntPredicate isOdd = isEven.negate(); boolean numbersAreOdd = numbers.stream().allMatch(isOdd);
IF you want to list all odd numbers, you should use Stream Filter # , for example:
numbers.stream().filter(isOdd).forEach(System.out::println);
IF you want to find the first odd number, you can use Stream # findFirst , for example:
Number odd = numbers.stream().filter(isOdd).findFirst().orElse(null);
IF you want to find the first odd number regardless of the order of the elements, you can use Stream # findAny , for example:
Number odd = numbers.stream().filter(isOdd).findAny().orElse(null);
Note: both Stream # findAny and Stream # findFirst are a terminal short-circuit operation , which means that it will exit after any intermediate operation .
source share