The andThen() and accept() methods are methods of Consumer Interfaces.
import java.util.function.Consumer; public class ConsumerInterfaceExample{ static void welcomeGreeting (Stringname){ System.out.println( "Welcome "+name ); } static void showAge (int age){ System.out.println( "Your age is "+age ); } static void showSalary (int sal){ System.out.println( "Your salary is "+sal ); } public static void main(String[] args) { //method reference Consumer<String> consumer1 = ConsumerInterfaceExample :: welcomeGreeting; // Calling Consumer method consumer1.accept( "Tejas" ); //method reference Consumer<Integer> consumer2 = ConsumerInterfaceExample::showAge; // Calling Consumer method consumer2.accept( 30 ); //method reference Consumer<Integer> consumer2 = ConsumerInterfaceExample :: showSalary; // Calling Consumer method consumer2.accept( 20000 ); } } Output: Welcome Tejas Your age is 30 Your salary is 20000
import java.util.function.Consumer; public class ConsumerInterfaceExample{ public static void main(String[] args) { //method reference Consumer<String> consumer1 = s -> System.out.println( "Welcome "+s ); Consumer<String> consumer2 = s -> System.out.println( "Welcome "+s+ "to the World!" ); Consumer<String> consumer2 = consumer1.andThen( consumer2 ); // Calling Consumer method consumer1.accept( "Tejas" ); } } Output: Welcome Tejas Welcome Tejas to the World!
The different between Consumer and Bi-Consumer is Consumer takes only one argument, but Bi-Consumer interface takes two arguments. Both, Consumer and Bi-Consumer have no return value. It is used in iterating through the entries of the map.
BiConsumer<String,String> foo = (a, b) -> System.out.println( a + b );