October 4, 2022

BiFunction Functional Interface Java Examples

In this post we’ll see examples of java.util.function.BiFunction functional interface.

BiFunction functional interface represents an operation that accepts two arguments and produces a result. Abstract method in this functional interface is-

R apply(T t, U u)- Where t is the first argument, u is the second function argument and R denotes the type of the result that is returned.

If you are writing a Lambda expression that takes two arguments of same or different types and returns a value of another type then that lambda expression can be written as an implementation of BiFunction built-in functional interface.

Apart from the apply() abstract method Function() interface has following default interface method.

andThen(Function<? super R,? extends V> after)- It takes another Function as argument and returns a composed BiFunction that performs, in sequence, first the operation of the calling BiFunction followed by the after operation.

BiFunction interface apply() method example

1. Writing a function that takes two Strings as arguments and returns concatenated String as result.

import java.util.function.BiFunction;

public class BiFunctionExample {
  public static void main(String[] args) {
    BiFunction<String, String, String> ref = (str1, str2) -> str1+ " " +str2;
    System.out.println("Concatenated Strings- " + ref.apply("Hello", "Lambda"));
  }
}
Output
Concatenated Strings- Hello Lambda

2. Writing a function that takes two Integers (i1, i2) as argument and returns a double value which is i1 to the power i2.

public class BiFunctionExample {
  public static void main(String[] args) {
    BiFunction<Integer, Integer, Double> biFunction = (i1, i2) -> Math.pow(i1, i2);
    System.out.println("Result- " + biFunction.apply(3, 4));
  }
}
Output
Result- 81.0

BiFunction functional interface andThen() method example

In the example there will be a BiFunction interface and a Function interface, on the BiFunction, andThen() is called with Function as an argument. When apply() method is called on the BiFunction it first executes the BiFunction implementation and then the Function implementation.

public class BiFunctionExample {
  public static void main(String[] args) {
    BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;
    Function<Integer, Integer> function = (a) -> a * a;
    // Sequence First BiFunction then Function
    System.out.println("Result- " + biFunction.andThen(function).apply(3, 5));
  }
}
Output
Result- 64

BiFunction functional interface in JDK

These built-in functional interfaces are used extensively with in the JDK itself. In Map interface compute, computeIfPresent, computeIfPresent methods are added in Java8 out of these methods compute and computeIfPresent take BiFunction as argument.

computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)- If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

ComputeIfPresent example
public class BiFunctionExample {
  public static void main(String[] args) {
      Map<String, Integer> numMap = new HashMap<String, Integer>();
      numMap.put("A", 1);
      numMap.put("B", 3);
      numMap.put("C", 8);
      numMap.put("D", 5);
      System.out.println("------- Before Computation -------");
      numMap.forEach((key, val)-> System.out.println("Key- " +key + "Val- " + val));
      numMap.computeIfPresent("B", (key, val)-> val + 1);
      System.out.println("------- After Computation -------");
      numMap.forEach((key, val)-> System.out.println("Key- " +key + "Val- " + val));
  }
}
Output
------- Before Computation -------
Key- AVal- 1
Key- BVal- 3
Key- CVal- 8
Key- DVal- 5
------- After Computation -------
Key- AVal- 1
Key- BVal- 4
Key- CVal- 8
Key- DVal- 5

That's all for the topic BiFunction Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment