Friday, August 23, 2019

Supplier And Consumer In Java 8

Supplier < T >:
Supplier is a in-built functional interface. It has been introduced in Java 8, to implement functional programming in Java. It's belong to java.util.function package.
Supplier Interface has one abstract method T get(), there is no input argument but return a value of type T.
Where T is the type of the result.

public interface Supplier<T> {
     T get();

Consumer < T >: Consumer is a in-built functional interface. it is present in  java.util.function. Consumer interface is just like Supplier interface but, it has one abstract method accept(T t) it's returns type void that means no result. And Consumer interface has one default method andThen(Consumer<? super T> after)

public interface Consumer<T> {
    void accept(T t);  // abstract method
    default Consumer<T> andThen(Consumer<? super T> after) {     // default method
        return (T t) -> { accept(t); after.accept(t); };

No comments:

Post a Comment