Back to Functional Interfaces
Q6Question 6 of 13

What is Predicate.not()?

Key Concepts
  • Predicate.not(target) is a static method that cleanly negates a Predicate: filter(Predicate.not(String::isBlank))
  • Cleaner than negated lambdas s -> !s.isBlank() or the verbose predicate.negate() pattern
  • Pattern.asPredicate() converts a compiled regex to a Predicate using find() for partial matching
  • Pattern.asMatchPredicate() converts a regex to a Predicate using matches() for full-string matching
  • All three produce Predicate<String> that integrates with stream filter and composes with and/or/negate
  • Pattern compilation happens once and the resulting Predicate is reused for thread safety
  • Java 11 for Predicate.not and asMatchPredicate; Java 8 for asPredicate

Definition

Predicate.not() cleanly negates a method reference for stream filtering, and Pattern.asPredicate/asMatchPredicate convert regex patterns to Predicates for find/matches checks.
  • Predicate.not(target) – Java 11 static method that negates a Predicate. filter(Predicate.not(String::isBlank)) reads as "filter: not blank".
  • Pattern.asPredicate() – Java 8; converts a compiled regex to a Predicate using matcher(s).find() (partial match).
  • Pattern.asMatchPredicate() – Java 11; converts a compiled regex to a Predicate using matcher(s).matches() (full-string match).
  • All produce Predicate – they integrate with stream filter, compose with and(), or(), negate(), and can be stored as constants.

Predicate.not() is like stamping a bank form "NOT blank" instead of writing "the opposite of blank": clearer and more professional. Pattern.asPredicate() gives the scanning machine a template to find any document that contains the pattern. asMatchPredicate() is stricter: the entire document must match.

Understanding the Concepts

What is Predicate.not()?

Three ways to negate a predicate; the third (Java 11) is cleanest.

JAVA
import java.util.function.Predicate;

// Way 1: negated lambda (verbose, easy to miss the !)
trades.stream().filter(s -> !s.isBlank());

// Way 2: .negate() (readable but longer)
Predicate<String> notBlank = ((Predicate<String>) String::isBlank).negate();

// Way 3: Predicate.not() (Java 11, cleanest)
trades.stream().filter(Predicate.not(String::isBlank));
// Reads as: "filter: NOT blank"

How do you use Predicate.not with other types?

JAVA
import java.util.function.Predicate;
import java.util.List;

// Filter non-settled trades
trades.stream().filter(Predicate.not(Trade::isSettled));

// Filter non-empty lists
accounts.stream().filter(Predicate.not(List::isEmpty));

// Compose predicates
Predicate<Trade> notSettledOrLarge = Predicate.not(Trade::isSettled)
    .or(t -> t.getAmount().compareTo(limit) > 0);

What is Pattern.asPredicate vs asMatchPredicate?

JAVA
import java.util.regex.Pattern;
import java.util.function.Predicate;

Pattern ifscPattern = Pattern.compile("[A-Z]{4}0[A-Z0-9]{6}");

// asPredicate: find() - string CONTAINS a match
Predicate<String> containsIfsc = ifscPattern.asPredicate();
containsIfsc.test("Code: SBIN0001234 here");  // true: found within string
containsIfsc.test("no match");                 // false

// asMatchPredicate: matches() - ENTIRE string must match
Predicate<String> isIfsc = ifscPattern.asMatchPredicate();
isIfsc.test("SBIN0001234");               // true: entire string matches
isIfsc.test("Code: SBIN0001234 here");    // false: extra text!

What is the comparison between asPredicate and asMatchPredicate?

| Method | Match type | Added in | Matcher method |
|--------|-----------|---------|----------------|
| asPredicate() | Partial (find) | Java 8 | matcher.find() |
| asMatchPredicate() | Full (match) | Java 11 | matcher.matches() |

How do you use regex predicates in stream filters?

JAVA
List<String> validCodes = inputs.stream()
    .filter(ifscPattern.asMatchPredicate())
    .toList();

// Multiple regex filters composed
Predicate<String> isIfsc = ifscPattern.asMatchPredicate();
Predicate<String> notBlank = Predicate.not(String::isBlank);

List<String> cleanValid = inputs.stream()
    .filter(notBlank.and(isIfsc))
    .toList();

What are common mistakes?

JAVA
// WRONG: recompiling pattern inside filter (expensive)
accounts.stream()
    .filter(id -> Pattern.matches("SAV\\d+", id)); // recompiles every call!

// CORRECT: compile once, reuse predicate
Predicate<String> isSavings = Pattern.compile("SAV\\d+").asMatchPredicate();
accounts.stream().filter(isSavings);

How does Predicate.not differ from negate()?

JAVA
// negate(): instance method, needs a Predicate variable
Predicate<String> blank = String::isBlank;
Predicate<String> notBlank = blank.negate();

// not(): static method, accepts method reference directly
Predicate.not(String::isBlank); // no intermediate variable needed

// Same result, different syntax

What should a senior engineer know?

  • Predicate.not() works with any method reference or lambda, not just String methods.
  • Pattern compilation is expensive; compile once and reuse. The resulting Predicate is thread-safe.
  • asPredicate uses find() (partial match); asMatchPredicate uses matches() (full match). Know the difference.
  • Predicate.not was added in Java 11 specifically to make method reference negation cleaner.
  • All Predicate variants compose with and(), or(), and negate() for complex filter logic.

Visual Overview

DIAGRAM
  Predicate.not(String::isBlank):

  Input:  ["", "SAV2001", "  ", "CUR3001", null]
  Filter: Predicate.not(String::isBlank)
  Output: ["SAV2001", "CUR3001"]

  Pattern.asPredicate vs asMatchPredicate:

  Pattern: [A-Z]{4}0[A-Z0-9]{6}
  Input: "Code: SBIN0001234 here"

  asPredicate (find):      true  ← found WITHIN string
  asMatchPredicate (match): false ← extra text around match

Key Takeaway

Predicate.not() (Java 11) is a static method for clean predicate negation. filter(Predicate.not(String::isBlank)) reads as "filter: not blank" and is cleaner than filter(s -> !s.isBlank()). It works with any method reference or Predicate.

Pattern.asMatchPredicate() (Java 11) converts a compiled regex into a Predicate that requires the entire string to match. Pattern.asPredicate() (Java 8) creates a Predicate that checks if the string contains a match anywhere. The key difference: asPredicate finds the pattern anywhere, asMatchPredicate requires the entire string to be the pattern.

All three produce Predicate that integrates seamlessly with stream filter, composes with and(), or(), and negate(), and can be stored as constants for reuse. Pattern compilation happens once and the resulting Predicate is thread-safe.

Remember one sentence: Predicate.not() cleanly negates method references, and Pattern.asMatchPredicate converts regex to full-match Predicates for stream filtering.

---

Share
Use ← → arrow keys to navigate