Languages
[Edit]
EN

Java 8 - filter collections with multiple conditions

0 points
Created by:
Brett4
465

In this article, we would like to show you how to filter collections using stream in Java 8.

1. Using lambda expression

Edit

In this example, we use stream filter() method with multiple conditions (&& operator) to make a list of bank accounts where:

  • the account number is greater than 2,
  • balance is more than 100 USD.

Practical example:

Output:

2. Using method reference

Edit

In this example, we use multiple filter() methods with method reference to make a list of bank accounts where:

  • the account number is greater than 2,
  • balance is more than 100 USD.

In this case, we need to add the following methods to the BankAccount class:

Practical example:

Output:

Example data

Edit

BankAccount.java:

Alternative titles

  1. Java 8 - filter collections with multiple criteria
  2. Java 8 - filter collections with complex condition
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join