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
Over 2 million developers have joined DZone.
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
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Trending

  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Microservices: Externalized Configuration
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  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 user avatar
Alex Theedom
·
Oct. 13, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.3K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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