Supplier and Consumer
Produce and consume values.
Supplier and Consumer is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Supplier Does
Supplier<T> takes no arguments and produces a value of type T.
- Its method is
T get(). - Perfect for lazy or deferred value creation.
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<String> greeting = () -> "Hello!";
System.out.println(greeting.get());
}
}Suppliers Are Lazy
The code inside a Supplier runs only when get is called. This makes suppliers ideal for expensive defaults you may never need.
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<Integer> expensive = () -> {
System.out.println("computing...");
return 42;
};
System.out.println("before get");
System.out.println(expensive.get());
}
}Supplier for Defaults
Many APIs accept a Supplier so they can compute a value only when needed, such as Optional.orElseGet.
import java.util.Optional;
public class Main {
public static void main(String[] args) {
Optional<String> name = Optional.empty();
System.out.println(name.orElseGet(() -> "default"));
}
}What Consumer Does
Consumer<T> accepts a value and returns nothing (void).
- Its method is
void accept(T t). - Used for side effects like printing or logging.
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Consumer<String> printer = s -> System.out.println("Got: " + s);
printer.accept("data");
}
}Consumer with forEach
The most common place you meet Consumer is Iterable.forEach and Stream.forEach.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> items = List.of("a", "b", "c");
items.forEach(item -> System.out.println(item));
}
}Chaining Consumers with andThen
Consumer supports andThen, which runs two consumers in sequence on the same input.
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Consumer<String> first = s -> System.out.println("upper: " + s.toUpperCase());
Consumer<String> second = s -> System.out.println("len: " + s.length());
first.andThen(second).accept("hello");
}
}BiConsumer for Two Inputs
BiConsumer<T, U> accepts two arguments and returns nothing. Map.forEach uses it to iterate key-value pairs.
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> ages = Map.of("Ann", 30, "Bob", 25);
ages.forEach((name, age) -> System.out.println(name + "=" + age));
}
}Method References as Consumers
A method reference such as System.out::println is a clean Consumer.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3);
nums.forEach(System.out::println);
}
}Combining Supplier and Consumer
A Supplier can feed its produced value straight into a Consumer, forming a simple produce-then-consume flow.
import java.util.function.Supplier;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Supplier<String> source = () -> "payload";
Consumer<String> sink = s -> System.out.println("received " + s);
sink.accept(source.get());
}
}Supplier Returning Objects
Constructors make natural suppliers via ClassName::new when no arguments are needed.
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<List<String>> factory = ArrayList::new;
List<String> list = factory.get();
list.add("x");
System.out.println(list);
}
}Side Effects vs Pure Functions
Remember the contrast:
Supplier: no input, produces output.Consumer: takes input, produces no output (side effect).Function: takes input and returns output.
import java.util.function.Supplier;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Supplier<Integer> dice = () -> 4;
Consumer<Integer> show = n -> System.out.println("rolled " + n);
show.accept(dice.get());
}
}Quick Check
Which functional interface takes no arguments and returns a value?
Recap
You produced and consumed values functionally:
Supplier<T>usesget()for lazy production.Consumer<T>usesaccept()for side effects and chains withandThen.BiConsumer<T,U>handles two inputs, as inMap.forEach.- Method references and constructor references make both concise.
Frequently asked questions
Is the “Supplier and Consumer” lesson free?
Yes — the full text of “Supplier and Consumer” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Supplier and Consumer”?
Produce and consume values. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Supplier and Consumer” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Function and BiFunction
- Supplier and Consumer
- Predicate and Composition
- Custom Functional Interfaces