Lesson 2
Consumers and Suppliers
Consumer and Supplier Interfaces in Java

Welcome to the lesson on "Consumers and Suppliers". As we continue to explore functional interfaces in Java, this lesson will introduce you to two key interfaces: Consumer and Supplier. Building on the foundational concepts you've learned so far, we'll dive into how these interfaces can be utilized effectively to make your code more modular and readable.

What You'll Learn

By the end of this lesson, you will feel comfortable using Consumer and Supplier interfaces to create more functional and modular code.

  • Using the Consumer interface to perform operations on input values.
  • Understanding the Supplier interface to handle data provisioning.
Overview of Consumer and Supplier Interfaces

In Java, functional interfaces like Consumer and Supplier play an important role in functional programming. Both of these interfaces are marked with the @FunctionalInterface annotation, which indicates that they have exactly one abstract method. This makes them ideal for use with lambda expressions.

  • Consumer Interface: Represents an operation that takes a single input argument and returns no result. It’s commonly used for operations like printing or logging, where you need to process data but don’t need to return anything.
  • Supplier Interface: Represents a function that supplies a result without taking any input arguments. It's useful in scenarios where you need to generate or provide data on demand.
Using the Consumer Interface

The following example demonstrates how to implement a simple Consumer that prints a message to the console:

Java
1@FunctionalInterface 2public interface Consumer<T> { 3 void accept(T t); 4} 5 6Consumer<String> printer = message -> System.out.println(message); 7printer.accept("Hello World"); // Output: Hello World

The Consumer interface allows you to define a block of code that performs operations on the received input. In this example, the printer is a consumer that prints the input message to the console. The lambda expression message -> System.out.println(message) accepts a string and prints it.

Implementing the Supplier Interface

The next example shows how to create a Supplier that supplies a predefined string value:

Java
1@FunctionalInterface 2public interface Supplier<T> { 3 T get(); 4} 5 6Supplier<String> supplier = () -> "Supplied value"; 7System.out.println(supplier.get()); // Output: Supplied value

The Supplier interface allows for the creation of objects on demand, without taking any input arguments. Here, the supplier is a supplier that returns a string when its get method is called. The lambda expression () -> "Supplied value" is used to provide the value.

Why It Matters

Understanding how to use the Consumer and Supplier interfaces enriches your functional programming skills, enabling you to:

  • Enhance Code Modularity: Break down complex tasks into smaller, reusable components.
  • Improve Readability: Use clear and concise code to perform specific operations.
  • Facilitate Data Generation: Easily create and manipulate data objects in various contexts.

By mastering these interfaces, you can write more fluent and readable Java code, making it easier to manage and scale your applications.

Ready to test your knowledge and skills? Let's move on to the practice section and get started!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.