Core Java

Java 8 Streams: filter and predicate negation

Recently there was an interesting discussion on the use of predicate negation in the .filter method on a stream by members of the LJC mailing list, so I thought it would be worth summarising it in a blog post. The discussion was about ways to use .filter and to negate the predicate.

This is perhaps how you might think about doing, but here are some alternative ways.

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

Answer 1: Write a predicate utility method

You can simplify this by writing a utility method that performs the negation.

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

Which results in much neater code.

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

Answer 2: Use an identity function to convert the method reference to a Predicate

We use a utility method to convert a method reference to a predicate.

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

although the code is not as neat.

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

Answer 3: Use the not (!) operator

Use the familiar not operator.

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

The code is much simpler and immediately familiar.

It is argued that method references are often harder to read and are trickier when refactoring than simple lambdas and that that mixing lambdas and method references in a Stream chain is confusing to the reader. Reference: Java SE 8 Best Practices

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.

Here are some useful references:

Reference: Java 8 Streams: filter and predicate negation from our JCG partner Alex Theedom at the alex.theedom blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Alex Theedom

Alex Theedom is a Senior Java Developer and has recently played a pivotal role in the architectural design and development of a microservice based, custom built lottery and instant win game platform. Alex has experience of Java web application development in a diverse range of fields including finance, e-learning, lottery and software development. He is the co-author of Professional Java EE Design Patterns and many articles.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button