Skip to content

What is Consumer Interface in Java?

This lesson talks about the first functional interface, which is the Consumer.

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

What is a Consumer Interface?

It takes a single argument and returns nothing, that is why it is called Consumer. Because it consumes a value.

In other words, represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, the Consumer is expected to operate via side effects.

This is a functional interface whose functional method is accept(Object).

Syntax:

Consumer<T>

Methods

This functional interface has two methods

  1. accept
  2. andThen

1. accept

Performs this operation on the given argument. Let us see an example snippet that converts the string to lowercase.

This is the proper and correct syntax.

Syntax:

void accept(T t);

Code

import java.util.function.Consumer;

public class ConsumerAccept {
  public static void main(String[] args) {
    print(100);
  }

  public static void print(int number) {
    Consumer<Integer> consumer = A -> System.out.println(A * 2);
    consumer.accept(number);
  }
}

2. andThen

This method returns a composed Consumer that performs, in sequence, this operation followed by the after operation.

If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

Syntax:

default Consumer<T> andThen(Consumer<? super T> after);

It is creating a new Consumer for each call to andThen, finally, at the end, it invokes the accept method which is the only abstract one.

Code

import java.util.function.Consumer;

public class ConsumerAndThen {
  public static void main(String[] args) {
    print(100, 2, 5);
  }

  public static void print(int A, int firstMultiplier, int secondMultiplier) {
    Consumer<Integer> firstConsumer = number -> System.out.println(number * firstMultiplier);
    Consumer<Integer> secondConsumer = number -> System.out.println(number * secondMultiplier);

    Consumer<Integer> thirdConsumer = firstConsumer.andThen(secondConsumer);

    // accept
    thirdConsumer.accept(A);
  }
}

In lines 9 and 10, we created two Consumers, firstConsumer and secondConsumer, that respectively multiply the given input 100 by 2 and 5.

We created our third Consumer in line 12, using the andThen(...) method.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.