Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 stream filter by multiple parameters

I have the following class:

public class Transfer {
    private String fromAccountID;
    private String toAccountID;
    private double amount;
}

and a List of Transfers:

....
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 Transfers. How can I do that with Java8 Stream filter functions?

like image 452
nsv Avatar asked Feb 05 '23 03:02

nsv


1 Answers

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());
like image 133
Ousmane D. Avatar answered Feb 06 '23 15:02

Ousmane D.