I'm fairly new to functional programming in Java, and I'm still experimenting with the code represented below.
I've created a method which for finding an Object within an Array using stream.
Method for finding an Account with provided clientIBAN:
public static Account findAccountByIBAN(String clientIBAN) {
return bankAccounts.stream()
.filter(account -> account.getIBAN()
.equals(clientIBAN)).findFirst()
.orElse(null);
}
I want to continue working with the result returned by this method.
My idea is to modify the result in the calling method. I want to know if there is a more elegant way to do that than the one I came up with?
Method that changes the state of the Account:
public static void depositToAccount(String clientIBAN, BigDecimal depositedAmount) {
findAccountByIBAN(clientIBAN)
.setAccountBalance(findAccountByIBAN(clientIBAN)
.getAccountBalance()
.add(depositedAmount));
}
Another option I was thinking of is just to iterate over accounts with a stream write inside depositToAccount() instead of calling findAccountByIBAN():
public static void depositToAccount(String clientIBAN, BigDecimal depositedAmount) {
bankAccounts.stream()
.filter(account -> account.getIBAN()
.equals(clientIBAN)).findFirst()
.ifPresent(balance ->
balance.setAccountBalance(balance.getAccountBalance().add(depositedAmount)));
}
The crux of my problem is that I want to keep iterating over that method with different things. I want to use filter() for my conditional logic instead of if-statements, but I cannot do that unless I can work with the result of my method as a stream.