Sunday, April 7, 2019

Supplier Functional Interface - JAVA 8

Supplier Functional Interface

Supplier is an in-built functional interface introduced in Java 8 in the java.util.function package. Supplier does not expect any input but returns the output. This interface can be handy where you want to generate some data like OTP, currentTime or even GUID
The functional method of Supplier is get(T t).
Here is a simple source code of java.util.function.Supplier

public interface Supplier<T> {
     * Gets a result.
     * @return a result
    T get();
Where T get () is an abstract method where T is the return type of the function.

Example 1:

import java.util.Date;
import java.util.function.Supplier;

public class SupplierExample {

       public static void main(String[] args) {

             // Calling Supplier Functional Interface without using Lambda Expression
             SystemDate date = new SystemDate();
             System.out.println("System Date: " + date.get());

             // Using Lambda Expression
             Supplier<Date> supplier = () -> new Date();
             System.out.println("Using lambda, System Date: " + supplier.get());


class SystemDate implements Supplier<Date> {

       public Date get() {
             return new Date();

System Date: Sun Apr 07 17:37:49 IST 2019
Using lambda, System Date: Sun Apr 07 17:37:49 IST 2019

This interface doesn’t contains any default or static method

That’s it for Supplier Functional Interface, Will talk about other Predefined Functional Interface in my next blog.

Happy Coding…!!!

No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...