Core Java

Java 8 Functional Interface – BiConsumer Example

Hello. In this tutorial, we will explain the BiConsumer functional interface introduced in java 8.

1. Introduction

java 8 BiConsumer

Before diving deep into the practice stuff let us understand the BiConsumer functional interface in java8 programming.

  • void accept(T t, U u) – It is an abstract method that accepts two input arguments, prints the operation based on the given input, and returns no result
  • BiConsumer andThen(BiConsumer after) – It is a default method in the functional interface that runs on two BiConsumer code one after another on the same input

2. Practice

Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed in your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Understanding BiConsumer interface

Create a java file in the com.java8 package and add the following code to it.

BiConsumerDemo.java

package com.java8;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/*
 * Java8 BiConsumer function interface represents an operation with two arguments 
 * and returns no result.
 * Syntax - accept(T t, U u);
 */
public class BiConsumerDemo {
	
	// example method 1
	private static void basic() {
		// BiConsumer lambda expression
		final BiConsumer<String, String> concat = 
				(val1, val2) -> System.out.println(val1+" "+val2);
		
		concat.accept("Hello", "world!");
	}

	// example method 2
	private static void printMap() {
		final Map<Integer, String> map = new HashMap<>();
		map.put(1, "Car");
		map.put(2, "Ship");
		map.put(3, "Bike");
		map.put(4, "Trolley");
		map.put(5, "Airplane");
		
		// BiConsumer lambda expression
		// print the key and value for a map
		final BiConsumer<Integer, String> print = 
				(val1, val2) -> System.out.println(val1+" "+val2);
		
		// using forEach() as it can accept the BiConsumer lambda expression
		map.forEach(print);
	}
	
	// example method 3
	private static void andThen() {
		// BiConsumer lambda expression
		final BiConsumer<Integer, Integer> addition =
				(val1, val2) -> System.out.println("Sum of input is= "+(val1 + val2));
				
		final BiConsumer<Integer, Integer> subtraction =
				(val1, val2) -> System.out.println("Subtraction of input is= "+(val1 - val2));
		
		// using andThen()
		// run the 2 logic one after another on the same input
		// if passing null to andThen() method it will throw NullPointerException
		addition.andThen(subtraction).accept(10, 5);
	}

	// driver method
	public static void main(String[] args) {
		System.out.println("----- BiConsumer functional interface example -----\n");
		
		basic();
		
		System.out.println("\n");
		
		printMap();
		
		System.out.println("\n");
		
		andThen();
	}
}

Run the file and if everything goes well the following output will be logged in the IDE console.

Console output

----- BiConsumer functional interface example -----

Hello world!


1 Car
2 Ship
3 Bike
4 Trolley
5 Airplane


Sum of input is= 15
Subtraction of input is= 5

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned the BiConsumer functional interface introduced in java8 programming along with the implementation. The functional interface consists of two methods that are widely used with the collection api. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on learning and implementing the BiConsumer functional interface in java8 programming.

Download
You can download the full source code of this example here: Java 8 Functional Interface – BiConsumer Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button