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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Complete Guide to Stream API and Collectors in Java 8
  • Setting Up Data Pipelines With Snowflake Dynamic Tables
  • Harnessing Real-Time Insights With Streaming SQL on Kafka
  • Thread-Safety Pitfalls in XML Processing

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  1. DZone
  2. Coding
  3. JavaScript
  4. Passing Multiple Arguments Into Stream Filter Predicates

Passing Multiple Arguments Into Stream Filter Predicates

Learn more about passing multiple arguments in Java.

By 
Niklas Schlimm user avatar
Niklas Schlimm
·
Mar. 21, 19 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
42.5K Views

Join the DZone community and get the full member experience.

Join For Free

When I am working with Java streams, I am intensively using filters to find objects. I often encounter the situation where I'd like to pass two arguments to the fiter function. But, unfortunately, the standard API only accepts a Predicate and not BiPredicate.

To solve this limitation, I define all of my predicates as methods in a class, for example, Predicates. Then, that predicate class takes a constant parameter.

public static class Predicates {
     private String pattern;
     public boolean containsPattern(String string) {
         return string.contains(pattern);
     }
     public Predicates(String pattern) {
         this.pattern = pattern;
     }
}


When I am using the Predicates, I instintiate an instance with the constant parameter of my choice. Then, I can use the instance methods as method references passed to the filter, like so:

Predicates predicates = new Predicates("SSH");
System.getenv().keySet().stream().filter(predicates::containsPattern).collect(Collectors.toSet());


This way, you can easily pass additional parameters to the filter, and your code is easy to read, even if you have multiple filters in the chain. Also, you can reuse the predicates in the Predicate class in other Collection operations.

Happy coding!

Filter (software) Stream (computing)

Opinions expressed by DZone contributors are their own.

Related

  • The Complete Guide to Stream API and Collectors in Java 8
  • Setting Up Data Pipelines With Snowflake Dynamic Tables
  • Harnessing Real-Time Insights With Streaming SQL on Kafka
  • Thread-Safety Pitfalls in XML Processing

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!