Interface Name | Description | Abstract Method |
---|
BinaryOperator<T> | Accepts two (reference type) input argument of same type and returns a result of same type. | T apply (T t1, T t2) |
DoubleBinaryOperator | Accepts two double-value argument and returns a double-value result. | double applyAsDouble(double d1, double d2) |
IntBinaryOperator | Accepts two int-value argument and returns an int-value result. | int applyAsInt(int i1, int i2) |
LongBinaryOperator | Accepts two long-value argument and returns a long-value result | long applyAsLong(long l1, long l2) |
T apply(T t1, T t2) is an abstruct method in Binary functional interface. It takes two input and return same type of result.
public class BankAccount {
private double balance;
public BankAccount(double balance){
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void transferFunds(BankAccount otherAccount, BinaryOperator<Double> transferFunction, double amount){
this.balance = transferFunction.apply(this.balance, amount);
otherAccount.balance = transferFunction.apply(otherAccount.balance, -amount);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account1 = new BankAccount(1000.0);
BankAccount account2 = new BankAccount(500.0);
BinaryOperator<Double> transferFunction = (balance, amount) -> balance + amount;
account1.transferFunds(account2, transferFunction, -200);
System.out.println("Account 1 Balance: $"+account1.getBalance());
System.out.println("Account 2 Balance: $"+account2.getBalance());
}
}