How to implement ToIntBiFunction<T, U> using lambda expression in Java?

Java 8Object Oriented ProgrammingProgramming

ToIntBiFunction<T, U> is a built-in functional interface from java.util.function package. This interface accepts two parameters as input and produces an int-valued result. ToIntBiFunction<T, U> interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt() and doesn't contain any default or static methods.

Syntax

@FunctionalInterface
interface ToIntBiFunction<T, U> {
   int applyAsInt(T t, U u);
}

Example

import java.util.function.ToIntBiFunction;
public class ToIntBiFunctionTest {
   public static void main(String args[]) {
      ToIntBiFunction<Integer, Integer> test = (t, u) -> t * u;
      System.out.println("The multiplication of t and u is: " + test.applyAsInt(10, 7));
      System.out.println("The multiplication of t and u is: " + test.applyAsInt(8, 15));
   }
}

Output

The multiplication of t and u is: 70
The multiplication of t and u is: 120
raja
Published on 23-Jan-2020 17:15:22
Advertisements