Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8203428

Predicate::not

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • core-libs
    • None
    • minimal
    • Java API
    • SE

      Summary

      Introduce a new static method Predicate::not which will allow developers to negate predicate lambdas trivially.

      Problem

      The requirement for predicate negation occurs frequently since predicates are defined antipodal to a positive selection; isNull, isEmpty, isBlank.

      Presently there is no easy way to negate a predicate lambda without first wrapping in a Predicate Object.

      Ex.
      
          List<String> list = bufferedReader
            .lines()
            .filter(((Predicate<String>)String::isEmpty).negate())
            .collect(toList()); 

      Solution

      Introduce a method that accepts an unwrapped lambda and returns the negated form.

      Ex.
      
      import static java.util.function.Predicate.not;
      ...
      
          List<String> list = bufferedReader
            .lines()
            .filter(not(String::isEmpty))
            .collect(toList()); 

      Specification

          /**
           * Returns a predicate that is the negation of the
           * supplied predicate.
           * @param <T>     the type of arguments to the
           *                specified predicate
           * @param target  predicate to negate
           *
           * @return a predicate that negates the results
           *         of the supplied predicate
           *
           * @since 11
           */
          static <T> Predicate<T> not(Predicate<? super T> target) {
              return (Predicate<T>)target.negate();
          }

            jlaskey Jim Laskey
            webbuggrp Webbug Group
            Chris Hegarty, Doug Lea, Paul Sandoz, RĂ©mi Forax, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: