Java 8 – BiConsumer Functional Interface

In this article, we will discuss pre-defined Functional Interface in Java 1.8 version for performing operation (processing) based on 2 input arguments passed but it won’t return any value after processing i.e.; BiConsumer Functional Interface

1. BiConsumer Functional Interface:

  • This Functional Interface has one abstract method called void accept(T t, U u); which
  • Step 1 :- accepts 2 input arguments of any data-type
  • Step 2 :- performs some operation or processes based on the 2 input arguments passed
  • Step 3 :- And doesn’t return result after processing (operation)
  • Below is the BiConsumer Functional Interface containing accept(); method along with other default & static methods
package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface BiConsumer<T, U> {

    /**
     * Performs this operation on the given arguments.
     *
     * @param t the first input argument
     * @param u the second input argument
     */
    void accept(T t, U u);

    // other default and static methods
}

2. Examples for BiConsumer Functional Interface :

Example 1 – program to print Student info along with increased grace marks of 5 for wrong question in the exam

package net.bench.resources.biconsumer.example;

import java.util.function.BiConsumer;

class Student {

	// member variables
	String name;
	int marks;

	// public constructor
	public Student(String name, int marks) {
		super();
		this.name = name;
		this.marks = marks;
	}
}

public class StudentResult {

	public static void main(String[] args) {

		// lambda expression to add grace marks to student using BiConsumer FI
		BiConsumer<Student, Integer> c = (s, i) -> s.marks = s.marks + 5;

		// create Student object with name and initial marks
		Student[] sArray = {
				new Student("Vijay", 92),
				new Student("Ajith", 78),
				new Student("Vikram", 66),
				new Student("Surya", 54),
				new Student("Karthi", 43),
				new Student("Arya", 31)
		};

		// invoke BiConsumer FI, iterating through all Students
		for(Student s : sArray) {
			c.accept(s, 5);
		}

		// printing to console
		for(Student s : sArray) {
			System.out.println("Name : " + s.name + "\tMarks : " + s.marks);
		}
	}
}

Output:

Name : Vijay	Marks : 97
Name : Ajith	Marks : 83
Name : Vikram	Marks : 71
Name : Surya	Marks : 59
Name : Karthi	Marks : 48
Name : Arya	Marks : 36

Example 2 – program to add 2 Integers using BiConsumer and also printing to console at the same time

package net.bench.resources.biconsumer.example;

import java.util.function.BiConsumer;

public class AddTwoNumbersUsingBiConsumer {

	public static void main(String[] args) {

		// lambda expression to add 2 numbers using BiConsumer FI
		BiConsumer<Integer, Integer> bc = 
				(i, j) -> System.out.println(i + " + " + j + " = " + (i+j));

				// add 2 numbers invoking above lambda expression
				bc.accept(7, 3);
				bc.accept(19, 2);
				bc.accept(20, 10);
				bc.accept(15, 25);
				bc.accept(1, 9);
	}
}

Output:

7 + 3 = 10
19 + 2 = 21
20 + 10 = 30
15 + 25 = 40
1 + 9 = 10

3. Difference between Consumer and BiConsumer Functional Interface :

  • Consumer :- It accepts 1 input argument and does some operation on the input argument passed but doesn’t return any value
  • BiConsumer :- Whereas it accepts 2 input arguments and does some operation on the 2 input arguments passed and doesn’t return any value
  • So, it is simple if we want to perform some operation based on single input argument and do not want to return any value then go for Consumer Functional Interface
  • But in case if we require to do some operation (or processing) on 2 input arguments and do not want to return any value then BiConsumer Functional Interface is the best option
package net.bench.resources.biconsumer.example;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class DifferenceInConsumerAndBiConsumer {

	public static void main(String[] args) {

		// 1. Consumer - lambda expression to print to console Square value of input
		Consumer<Integer> c = (i) -> System.out.println(
				"Square of " + i + " is : " + i*i
				);

		// 2. BiConsumer - lambda expression to add both input arguments and print to console
		BiConsumer<Integer, Integer> bc = (i,j) -> System.out.println(
				"Sum of (" + i + ", "+ j + ") is : " + (i+j)
				);

		// 1.A test for Consumer
		System.out.println("Consumer testing for printing Square value : \n");
		c.accept(8);
		c.accept(19);

		// 2.A test for BiConsumer
		System.out.println("\nBiConsumer testing for both numbers : \n");
		bc.accept(7, 3);
		bc.accept(19, 43);
	}
}

Output:

Consumer testing for printing Square value : 

Square of 8 is : 64
Square of 19 is : 361

BiConsumer testing for both numbers : 

Sum of (7, 3) is : 10
Sum of (19, 43) is : 62

References:

Happy Coding !!
Happy Learning !!

Java 8 - Primitive Predicate Functional Interface
Java 8 - BiFunction Functional Interface