DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Build a Query in MuleSoft With Optional Parameters
  • Custom Rate Limiting for Microservices
  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook

Trending

  • AI Coding Agent Guardrails: Finding Gaps in Tool Permissions
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption
  • Article Moderation: Your Questions, Answered

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Four Ways to Negate a Predicate in the Steam API's Filter Method

Four Ways to Negate a Predicate in the Steam API's Filter Method

In Java there are various ways to negate a predicate in the Stream API's filter method. In this post I present four approaches.

By 
Alex Theedom
·
Oct. 13, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

There are various ways to negate the predicate passed to the filter method of the Stream API. In this blog post, I present four ways to  code this negation. 

1. Use the Negate Method of the Predicate API

The first approach is to take advantage of the Predicate API's negate() method. This is perhaps how you might have thought about doing it first off and would look something like the following:

Java
xxxxxxxxxx
1
 
1
Stream.of(1, 2, 3, 4, 5, 6, 7).filter(((Predicate) c -> c % 2 == 0).negate());


but here are some alternative approaches.

2. Write a Predicate Utility Method

You can simplify this approach by writing a utility method that is capable of performing the negation.

Java
xxxxxxxxxx
1
 
1
public static <R> Predicate<R> not(Predicate<R> predicate) {
2
    return predicate.negate();
3
}


Which results in much neater code.

Java
xxxxxxxxxx
1
 
1
Stream.of(1, 2, 3, 4, 5, 6, 7).filter(not(c -> c % 2 == 0));


The utility method can of course be reused throughout the application.

3. Use an Identity Function to Convert the Method Reference to a Predicate

With this approach you use a utility method to convert a method reference to a predicate.

Java
xxxxxxxxxx
1
 
1
public static <T> Predicate<T> predicate(Predicate<T> predicate) {
2
    return predicate;
3
}


although the code is not as neat.

Java
xxxxxxxxxx
1
 
1
Stream.of("Cat", "", "Dog").filter(predicate(String::isEmpty).negate());


References: Heinz’s Lambda Reduction Principle

4. Use the Not (!) Operator

Finally you can use the familiar, not operator.

Java
xxxxxxxxxx
1
 
1
Stream.of(1, 2, 3, 4, 5, 6, 7).filter((c -> c % 2 != 0));
2
3
Stream.of("Cat", "", "Dog").filter(str -> !str.isEmpty());


The code is much simpler and immediately familiar.

Conclusion

It is argued that method references are often harder to read and are trickier when refactoring than simple lambdas and that mixing lambdas and method references in a Stream chain is confusing to the reader. 

When you use a method reference and want the IDE to create the method, IntelliJ creates this as a static method with the object as the first argument. Using the not operator avoids this.

API Filter (software)

Published at DZone with permission of Alex Theedom. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Build a Query in MuleSoft With Optional Parameters
  • Custom Rate Limiting for Microservices
  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook