Supplier Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function doesn’t expect any input but produces an output in the form of a value of type T. In this post, we are going to see several implementations of Supplier Interface by using different examples.

Supplier_Interface_Java8_Techndeck

 

Supplier Interface in Java 8 Examples...!!! Share on X

Look at Supplier Javadoc description below:

SupplierInterface_Signature_Java8_Techndeck

As you’ve seen in the above screenshot, Supplier Interface contains only the following function:

T get();

This signifies that this method doesn’t take any argument as an input but produces an output value of type T.

Several Implementations of Supplier Interface in Java 8...!!! Share on X

 

Let’s begin:

Example 1. Supplier resulting a string value

import java.util.function.Supplier;

public class SupplierInterfaceJava8Example1 {

    public static void main(String args[]) {

        System.out.println("Ex. 1 - Java 8 Supplier Interface\n");

        Supplier <String> simpleString = () -> "Hello Basic Supplier";

        System.out.println(simpleString.get());
    }
}

 

Example 2. Supplier resulting a HashMap

import java.util.HashMap;
import java.util.function.Supplier;

public class SupplierInterfaceJava8Example2 {

    public static void main(String[] args) {

        System.out.println("Ex. 2 - Java 8 Supplier Interface\n");

        HashMap <Integer, String> personHashMap = new HashMap <Integer, String> ();

        personHashMap.put(19, "Isha");
        personHashMap.put(43, "Gaurangee");
        personHashMap.put(4, "Yashika");

        Supplier <HashMap <Integer, String>> supplierPersonObj = () -> personHashMap;

        printMap(supplierPersonObj);

    }

    public static void printMap(Supplier < HashMap < Integer, String >> supplierObj) {
        System.out.println(supplierObj.get());
    }
}

 

Example 3. Supplier resulting an ArrayList using Stream API 

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class SupplierInterfaceJava8Example3 {

    public static void main(String[] args) {

        System.out.println("Ex. 3 - Java 8 Supplier Interface\n");

        List <String> studentNames = new ArrayList <String> ();

        studentNames.add("Yogi");
        studentNames.add("Yash");
        studentNames.add("Yashika");
        studentNames.add("Pulkit");
        studentNames.add("Noni");
        studentNames.add("Isha");
        studentNames.add("DV");

        studentNames.stream().forEach((item) -> {
            printName(() -> item);
        });
    }

    public static void printName(Supplier <String> supplier) {
        System.out.println(supplier.get());
    }
}

 

Example 4. Supplier resulting a custom Class object (like ‘Students’ in this case)

import java.util.function.Supplier;
import org.personal.samples.Students;

public class SupplierInterfaceJava8Example4 {

    public static void main(String[] args) {

        System.out.println("Ex. 4 - Java 8 Supplier Interface\n");

        Supplier <Students> supplierStudentObj = () -> {
            return new Students(25, "Isha Durani");
        };

        System.out.println("Student details: \n" + "Name: " + supplierStudentObj.get().getName() + "\nAge: " + supplierStudentObj.get().getAge());
    }
}

 

Students Class:

public class Students {

    int age;
    String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Students() {}

    public Students(int age, String name) {
        this.age = age;
        this.name = name;
    }

}

 

Java 8 Supplier Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

 

Supplier Interface in Java 8 Examples...!!! Share on X

 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

 

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts