EN
Java 8 - filter collections with multiple conditions
0 points
In this article, we would like to show you how to filter collections using stream in Java 8.
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:
xxxxxxxxxx
1
import java.io.*;
2
import java.util.ArrayList;
3
import java.util.List;
4
import java.util.stream.Collectors;
5
6
7
public class Example {
8
9
public static void main(String[] args) throws IOException {
10
BankAccount account1 = new BankAccount(1, 100);
11
BankAccount account2 = new BankAccount(2, 101);
12
BankAccount account3 = new BankAccount(3, 102);
13
14
List<BankAccount> accounts = new ArrayList<>();
15
accounts.add(account1);
16
accounts.add(account2);
17
accounts.add(account3);
18
19
List<BankAccount> accountsWithMoreThan100USD = accounts
20
.stream()
21
.filter(x -> x.getBalance() > 100 && x.getAccountNumber() > 2)
22
.collect(Collectors.toList());
23
24
System.out.println(accountsWithMoreThan100USD);
25
}
26
}
Output:
xxxxxxxxxx
1
[BankAccount{ number=3, balance=102 USD }]
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:
xxxxxxxxxx
1
public boolean hasMoreThan100USD() {
2
return this.balance > 100;
3
}
4
5
public boolean isMoreThanTwo() {
6
return this.accountNumber > 2;
7
}
Practical example:
xxxxxxxxxx
1
import java.io.*;
2
import java.util.ArrayList;
3
import java.util.List;
4
import java.util.stream.Collectors;
5
6
7
public class Example {
8
9
public static void main(String[] args) throws IOException {
10
BankAccount account1 = new BankAccount(1, 100);
11
BankAccount account2 = new BankAccount(2, 101);
12
BankAccount account3 = new BankAccount(3, 102);
13
14
List<BankAccount> accounts = new ArrayList<>();
15
accounts.add(account1);
16
accounts.add(account2);
17
accounts.add(account3);
18
19
List<BankAccount> accountsWithMoreThan100USD = accounts
20
.stream()
21
.filter(BankAccount::isMoreThanTwo)
22
.filter(BankAccount::hasMoreThan100USD)
23
.collect(Collectors.toList());
24
25
System.out.println(accountsWithMoreThan100USD);
26
}
27
}
Output:
xxxxxxxxxx
1
[BankAccount{ number=3, balance=102 USD }]
BankAccount.java:
xxxxxxxxxx
1
public class BankAccount {
2
private final int accountNumber;
3
// for simplicity reason we keep USD in int
4
private final int balance;
5
6
public BankAccount(int accountNumber, int balance) {
7
this.accountNumber = accountNumber;
8
this.balance = balance;
9
}
10
11
public int getAccountNumber() {
12
return accountNumber;
13
}
14
15
public double getBalance() {
16
return balance;
17
}
18
19
public boolean hasMoreThan100USD() {
20
return this.balance > 100;
21
}
22
23
public boolean isMoreThanTwo() {
24
return this.accountNumber > 2;
25
}
26
27
28
public String toString() {
29
return "BankAccount{ " +
30
"number=" + accountNumber +
31
", balance=" + balance +
32
" USD }";
33
}
34
}