Supplier and Consumer in Java 8

Introduction

We will discuss here about the new feature added to Java 8 – a functional interface, Supplier and Consumer. In simple words, a supplier is a method that returns a value. A supplier is any method which takes no arguments and returns a value. Its job is to supply an instance of an expected class.

Whereas, a consumer is a method that consumes some value (as in method argument), and does some operations on them. So a Consumer is any method which takes arguments and returns nothing. It is invoked for its side-effects. In Java terms, a Consumer is an idiom for a void method. ‘setter’ methods are good examples of consumers.

Supplier

For example, every reference to a getter method is a supplier.

public class MyClass {
	private Integer count;

	public Integer getCount() {
		return this.count;
	}
}

Its instance method reference myClass::getCount is an instance of Supplier.

Supplier functional interface in Java 8 under package java.util.function represents the structure and does not take any input but returns an output. This functional interface can be used as the assignment target for a lambda expression or method reference. It’s written in the following manner – Java source documentation:

@FunctionalInterface
public interface Supplier<T> {

  T get();

}

The purpose of this in-built functional interface, Supplier, is to provide a ready-made template for functional interface having common function descriptor(functional method signature/definition).

The functional interfaces defined under package java.util.function do not have semantics and they merely represent the structure of a function that returns a value or takes number of arguments.

The Supplier interface also have a method T get() that returns a value of of type T.

A supplier is a way to create instance of a method that returns something or in other words its job is literally to supply an instance of an expected class.

Supplier functional interface doesn’t take any input but returns an output. Therefore whenever you need a function that returns something, for example, an Integer, but takes no output, this is an instance of Supplier.

You can have a look at examples on how to use supplier – REST over Https with Client certificate Authentication and Event Driven Streaming using Spring Cloud Stream and Apache Kafka.

Consumer

For example, every reference to a setter method is consumer:

public class MyClass {
	private Integer count;

	public void setCount(Integer count){
		this.count = count;
	}
}

Its instance method reference myClass::setCount is an instance of consumer.

So, if you want to input an Integer and do something with it with no output, then instead of defining your own interface use an instance of Consumer.

Consumer functional interface in Java 8 under package java.util.function represents the structure and takes input but does not return any output. This functional interface can be used as the assignment target for a lambda expression or method reference. It’s written in the following manner – Java source documentation:

@FunctionalInterface
public interface Consumer<T> {

  void accept(T t);

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

}

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects. This is a functional interface whose functional method is accept(Object).

The default 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.

Examples – Supplier and Consumer

Supplier

package com.roytuts.java.supplier.consumer;

import java.util.function.Supplier;

public class SupplierExample {

	public static void main(String[] args) {
		supply(() -> "Hi");
		supply(() -> "Hey");
		supply(() -> "Hello");
	}

	public static void supply(Supplier<?> supplier) {
		System.out.println(supplier.get());
	}

}

Output:

Hi
Hey
Hello

Consumer

package com.roytuts.java.supplier.consumer;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerExample {

	public static void main(String[] args) {
		List<String> greets = Arrays.asList("Hi", "Hey", "Hello");

		Consumer<String> consumer = new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s);
			}
		};

		greets.forEach(consumer);
	}

}

Output:

Hi
Hey
Hello

Thanks for reading.

Related posts

Leave a Comment