Java8 stream filter by multiple parameters

Sarvar Nishonboev :

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?

Ousmane D. :

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());

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443365&siteId=1