Saturday, June 13, 2026
HomeLanguagesJavaPredicate.not() Method in Java with Examples

Predicate.not() Method in Java with Examples

In order to negate an existing predicate, the Predicate.not() static method added to Java 11. The Predicate class is present in java.util.function package.

Syntax:

negate = Predicate.not( positivePredicate );

Parameters:

  • Predicate whose negate is required

Return Type: Return type not() method is Predicate.

Approach:

  1. Create one predicate and initialize the conditions to it.
  2. Create another predicate to create negate and assign it with the not() method.

Below is the implementation of the above approach:

Java




// Implementation of Predicate.not() method in Java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
  
public class GFG {
    public static void main(String[] args)
    {
        List<Integer> list
            = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
        // creating a predicate for negation
        Predicate<Integer> even = i -> i % 2 == 0;
  
        // creating a predicate object which
        // is negation os supplied predicate
        Predicate<Integer> odd = Predicate.not(even);
  
        // filtering the even number using even predicate
        List<Integer> evenNumbers
            = list.stream().filter(even).collect(
                Collectors.toList());
  
        // filtering the odd number using odd predicate
        List<Integer> oddNumbers
            = list.stream().filter(odd).collect(
                Collectors.toList());
        // Print the Lists
        System.out.println(evenNumbers);
        System.out.println(oddNumbers);
    }
}


Output:

[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS