I have the following class:
public class Transfer {
private String fromAccountID;
private String toAccountID;
private double amount;
}
and a List
of Transfer
s:
....
private List<Transfer> transfers = new ArrayList<>();
I know how to get one transfer history:
transfers.stream().filter(transfer ->
transfer.getFromAccountID().equals(id)).findFirst().get();
But I want to get by fromAccountID
and toAccountID
, so the result will be a List
of Transfer
s. How can I do that with Java8 Stream
filter functions?
filter by the two properties and collect into a list.
List<Transfer> resultSet =
transfers.stream().filter(t -> id.equals(t.getFromAccountID()) ||
id.equals(t.toAccountID()))
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With