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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • SRE vs. DevOps

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • SRE vs. DevOps
  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.

Alex Theedom user avatar by
Alex Theedom
·
Oct. 13, 20 · Tutorial
Like (2)
Save
Tweet
Share
3.85K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
  • SRE vs. DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: