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

  • Querying Without a Query Language
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value
  • Building a 300 Channel Video Encoding Server
  • Event-Driven Architecture's Dark Secret: Why 80% of Event Streams Are Wasted Resources

Trending

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Build Self-Managing Data Pipelines With an LLM Agent
  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
43.0K 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

  • Querying Without a Query Language
  • From Compliance Pipes to Data Streams: Modernizing Healthcare EDI for Strategic Value
  • Building a 300 Channel Video Encoding Server
  • Event-Driven Architecture's Dark Secret: Why 80% of Event Streams Are Wasted Resources

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