The BiFunction interface in Java is a functional interface introduced in Java 8 under the java.util.function package. It is designed to take two arguments of specified types, perform a computation on them, and return a result of another specified type.
Below are the key concepts and usage examples to understand and use the BiFunction interface:
BiFunction Interface Structure
It has a single abstract method:
R apply(T t, U u);
- T: The type of the first argument.
- U: The type of the second argument.
- R: The type of the resulting value.
Basic Usage Example
The apply method is used to define the logic. Here’s an example of adding two integers using a BiFunction:
package org.kodejava.util.function;
import java.util.function.BiFunction;
public class BiFunctionExample {
public static void main(String[] args) {
// Create a BiFunction to add two numbers
BiFunction<Integer, Integer, Integer> addFunction =
(a, b) -> a + b;
// Use the BiFunction
int result = addFunction.apply(5, 10);
// Output: Result: 15
System.out.println("Result: " + result);
}
}
Combining BiFunction with Other Functions
The BiFunction interface also provides a default method named andThen. This allows us to perform further operations on the output of a BiFunction.
Example:
package org.kodejava.util.function;
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiFunctionAndThenExample {
public static void main(String[] args) {
// Create a BiFunction to multiply two numbers
BiFunction<Integer, Integer, Integer> multiplyFunction =
(a, b) -> a * b;
// Create a Function to square a number
Function<Integer, Integer> squareFunction =
number -> number * number;
// Combine them using andThen
int result = multiplyFunction
.andThen(squareFunction).apply(3, 4);
// Output: Result: 144 (3*4=12, 12^2=144)
System.out.println("Result: " + result);
}
}
Practical Use Cases of BiFunction
Processing Data
We can use BiFunction to process two pieces of related data and compute the result. For example, calculating a student’s grade based on a score and maximum score:
package org.kodejava.util.function;
import java.util.function.BiFunction;
public class StudentGrade {
public static void main(String[] args) {
// BiFunction to calculate the grade percentage
BiFunction<Integer, Integer, Double> calculateGradePercentage =
(score, maxScore) -> (score * 100.0) / maxScore;
double grade = calculateGradePercentage.apply(85, 100);
// Output: Grade: 85.0%
System.out.println("Grade: " + grade + "%");
}
}
Manipulating Strings
For situations like concatenating or formatting two strings:
package org.kodejava.util.function;
import java.util.function.BiFunction;
public class StringManipulation {
public static void main(String[] args) {
// BiFunction to concatenate two strings with a space
BiFunction<String, String, String> concatenateFunction =
(str1, str2) -> str1 + " " + str2;
String fullName = concatenateFunction.apply("John", "Doe");
// Output: Full Name: John Doe
System.out.println("Full Name: " + fullName);
}
}
Working With Collections
A BiFunction can be used to interact with collections, such as updating values in a map.
package org.kodejava.util.function;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
public class MapUpdateExample {
public static void main(String[] args) {
// A map with initial values
Map<String, Integer> salaries = new HashMap<>();
salaries.put("Alice", 3000);
salaries.put("Bob", 2500);
// BiFunction to update the salary values
BiFunction<String, Integer, Integer> salaryIncrease =
(name, currentSalary) -> currentSalary + 500;
// Update salaries
salaries.replaceAll(salaryIncrease);
// Output: {Alice=3500, Bob=3000}
System.out.println(salaries);
}
}
Chaining and Combining BiFunctions
We can combine multiple BiFunctions for complex computations. Here’s an example:
package org.kodejava.util.function;
import java.util.function.BiFunction;
public class BiFunctionChaining {
public static void main(String[] args) {
// First BiFunction: Adds two numbers
BiFunction<Integer, Integer, Integer> add =
(a, b) -> a + b;
// Second BiFunction: Multiplies two numbers
BiFunction<Integer, Integer, Integer> multiply =
(a, b) -> a * b;
// Combine: Add first, then multiply
int result = add.andThen(product ->
multiply.apply(product, 2)).apply(3, 4);
// Output: 14 (3+4=7, 7*2=14)
System.out.println("Result: " + result);
}
}
Key Points to Remember
- The
BiFunctioninterface is suitable for handling scenarios where two input arguments are needed to produce a single result. - It is often used in lambda expressions and method references for brevity.
- The
andThenmethod allows chaining to process the result further. - It is part of the
java.util.functionpackage, introduced in Java 8.
