BiConsumer Functional Interface in Java

The Consumer that can take a single input argument, performs some operation on that argument and doesn’t return any result. If we want to provide two arguments to the Consumer. It won’t take two arguments. That’s why Java 8 introduced the BiConsumer predefined functional interface. This functional interface takes two arguments, performs some operation on those arguments and doesn’t return any result. This functional interface is available in the “java.util.function” package.
Prototype:

public interface BiConsumer<T, U>{
void accept(T t, U u);
}


Type Parameters:
   T – It is a type of the first argument to the operation.
   U – It is a type of the second argument to the operation.

Methods of BiConsume Interface:
    1. accept()
    2. andThen()

1. accept():
This method is a SAM(Single Abstract Method) of the interface. This method takes two arguments and performs some operation on that arguments and doesn’t return any result.
Syntax:

void accept(T t, U u);


Types of Parameters:
  T – It is a type of the first argument.
  U – It is a type of the second argument.
Example: 

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

class Employee {
	String name;
	double salary;

	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
}
public class Test {
	public static void main(String[] args) {
		List<Employee> empl = new ArrayList<Employee>();
		prepareEmpList(empl);
		System.out.println("----Without Salary Increment of Employees----");
		for (Employee e : empl) {
			System.out.println("Employee Name:" + e.name);
			System.out.println("Employee Salary:" + e.salary);
			System.out.println();
		}
	BiConsumer<Employee, Double> biIncSal = (e, d) -> e.salary = e.salary + d;
		for (Employee e : empl) {
			biIncSal.accept(e, 500.0);
		}
	System.out.println("----With Salary Increment of Employees----");
		for (Employee e : empl) {
			System.out.println("Employee Name:" + e.name);
			System.out.println("Employee Salary:" + e.salary);
			System.out.println();
		}
	}
	public static void prepareEmpList(List<Employee> empList) {
		empList.add(new Employee("Peter", 1000));
		empList.add(new Employee("Robert", 1500));
		empList.add(new Employee("William", 2000));
		empList.add(new Employee("John", 2500));
		empList.add(new Employee("Ramesh", 3000));
	}
}

Output: 

----Without Salary Increment of Employees----
Employee Name:Peter
Employee Salary:1000.0

Employee Name:Robert
Employee Salary:1500.0

Employee Name:William
Employee Salary:2000.0

Employee Name:John
Employee Salary:2500.0

Employee Name:Ramesh
Employee Salary:3000.0

----With Salary Increment of Employees----
Employee Name:Peter
Employee Salary:1500.0

Employee Name:Robert
Employee Salary:2000.0

Employee Name:William
Employee Salary:2500.0

Employee Name:John
Employee Salary:3000.0

Employee Name:Ramesh
Employee Salary:3500.0


2. andThen():
This method returns a composed BiConsumer wherein the parameterized BiConsumer will be executed after the first one.
Syntax:

default BiConsumer <T, U> andThen(BiConsumer<? super T, ? super U> after){
}


Parameters:
after – It is the operation to perform after this operation.
Returns:
The composed BiConsumer performs this in the sequence of this operation followed by the after an operation
Throws:
NullPointerException – if the after is null
Example:

package test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

class Employee {
	String name;
	double salary;

	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
}
public class Test {

	public static void main(String[] args) {
		List<Employee> empl = new ArrayList<Employee>();
		prepareEmpList(empl);
		System.out.println("----Without Salary Increment and Bonus of Employees----");
		for (Employee e : empl) {
			System.out.println("Employee Name:" + e.name);
			System.out.println("Employee Salary:" + e.salary);
			System.out.println();
		}
		// This is used for Salary Increment
		BiConsumer<Employee, Double> biIncSal = (e, d) -> e.salary = e.salary + d;
		// This is used for Bonus
		BiConsumer<Employee, Double> biBonus = (e, d) -> e.salary = e.salary + 100.0;
		for (Employee e : empl) {
			biIncSal.andThen(biBonus).accept(e, 500.0);
		}
		System.out.println("----With Salary Increment and Bonus of Employees----");
		for (Employee e : empl) {
			System.out.println("Employee Name:" + e.name);
			System.out.println("Employee Salary:" + e.salary);
			System.out.println();
		}
	}
	public static void prepareEmpList(List<Employee> empList) {
		empList.add(new Employee("Peter", 1000));
		empList.add(new Employee("Robert", 1500));
		empList.add(new Employee("William", 2000));
		empList.add(new Employee("John", 2500));
		empList.add(new Employee("Ramesh", 3000));
	}
}

Output: 

----Without Salary Increment and Bonus of Employees----
Employee Name:Peter
Employee Salary:1000.0

Employee Name:Robert
Employee Salary:1500.0

Employee Name:William
Employee Salary:2000.0

Employee Name:John
Employee Salary:2500.0

Employee Name:Ramesh
Employee Salary:3000.0

----With Salary Increment and Bonus of Employees----
Employee Name:Peter
Employee Salary:1600.0

Employee Name:Robert
Employee Salary:2100.0

Employee Name:William
Employee Salary:2600.0

Employee Name:John
Employee Salary:3100.0

Employee Name:Ramesh
Employee Salary:3600.0


Conclusion:
This topic is explained What is a BiConsumer Interface in Java 8? What are the methods of this Interface? How to use this Function Interface in Java 8?

Leave a Comment