Languages
[Edit]
EN

Java 8 - filter collections with multiple conditions

0 points
Created by:
Dirask Community
6810

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

1. Using lambda expression

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:

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class Example {

    public static void main(String[] args) throws IOException {
        BankAccount account1 = new BankAccount(1, 100);
        BankAccount account2 = new BankAccount(2, 101);
        BankAccount account3 = new BankAccount(3, 102);

        List<BankAccount> accounts = new ArrayList<>();
        accounts.add(account1);
        accounts.add(account2);
        accounts.add(account3);

        List<BankAccount> accountsWithMoreThan100USD = accounts
                .stream()
                .filter(x -> x.getBalance() > 100 && x.getAccountNumber() > 2)
                .collect(Collectors.toList());

        System.out.println(accountsWithMoreThan100USD);
    }
}

Output:

[BankAccount{ number=3, balance=102 USD }]

2. Using method reference

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:

public boolean hasMoreThan100USD() {
    return this.balance > 100;
}

public boolean isMoreThanTwo() {
    return this.accountNumber > 2;
}

Practical example:

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class Example {

    public static void main(String[] args) throws IOException {
        BankAccount account1 = new BankAccount(1, 100);
        BankAccount account2 = new BankAccount(2, 101);
        BankAccount account3 = new BankAccount(3, 102);

        List<BankAccount> accounts = new ArrayList<>();
        accounts.add(account1);
        accounts.add(account2);
        accounts.add(account3);

        List<BankAccount> accountsWithMoreThan100USD = accounts
                .stream()
                .filter(BankAccount::isMoreThanTwo)
                .filter(BankAccount::hasMoreThan100USD)
                .collect(Collectors.toList());

        System.out.println(accountsWithMoreThan100USD);
    }
}

Output:

[BankAccount{ number=3, balance=102 USD }]

Example data

BankAccount.java:

public class BankAccount {
    private final int accountNumber;
    // for simplicity reason we keep USD in int
    private final int balance;

    public BankAccount(int accountNumber, int balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    public boolean hasMoreThan100USD() {
        return this.balance > 100;
    }

    public boolean isMoreThanTwo() {
        return this.accountNumber > 2;
    }

    @Override
    public String toString() {
        return "BankAccount{ " +
                "number=" + accountNumber +
                ", balance=" + balance +
                " USD }";
    }
}
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