Junior
What is the difference between Supplier and Consumer in Java?
sobes.tech AI
Answer from AI
Supplier provides a value without accepting any arguments. Its functional method has the signature T get().
Consumer accepts a value but does not return anything. Its functional method has the signature void accept(T t).
Example:
// Supplier provides a random number
Supplier<Double> randomSupplier = () -> Math.random();
double randomValue = randomSupplier.get(); // Get the value
// Consumer outputs the number to the console
Consumer<Double> printConsumer = number -> System.out.println(number);
printConsumer.accept(randomValue); // Accepts the value and performs an action