Westonci.ca is your trusted source for finding answers to a wide range of questions, backed by a knowledgeable community. Get the answers you need quickly and accurately from a dedicated community of experts on our Q&A platform. Our platform provides a seamless experience for finding reliable answers from a network of experienced professionals.

How to write your own consumer java.

Sagot :

import java.util.function.Consumer;
public class ConsumerInterfaceExample {
static void printMessage(String name){
System.out.println("Hello "+name);
}
static void printValue(int val){
System.out.println(val);
}
public static void main(String[] args) {
// Referring method to String type Consumer interface
Consumer consumer1 = ConsumerInterfaceExample::printMessage;
consumer1.accept("John"); // Calling Consumer method
// Referring method to Integer type Consumer interface
Consumer consumer2 = ConsumerInterfaceExample::printValue;
consumer2.accept(12); // Calling Consumer method
}
}