Sobes.tech
Junior
202

Mis vahe on Supplier ja Consumer vahel Java-s

Vastus AI-lt

sobes.tech AI

Supplier provides a value without accepting 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 prints the number to the console
Consumer<Double> printConsumer = number -> System.out.println(number);
printConsumer.accept(randomValue); // Accepts the value and performs the action