Java 8 Supplier Interface Example

In this tutorial, we will learn how to use the Supplier functional interface with an example.

Video Tutorial

Overview

java.util.function.Supplier is a functional interface whose functional method is R get(). The Supplier interface represents an operation that takes no argument and returns a result R:
@FunctionalInterface
public interface Supplier<T> {
    T get();
}

Supplier Interface Example #1

The following example shows how to use the get() method of the Supplier interface with Lambda expression.
package com.javaguides.java.functionalinterfaces;

import java.time.LocalDateTime;
import java.util.function.Supplier;

public class SupplierDemo {

    public static void main(String[] args) {
        Supplier < LocalDateTime > supplier = () - > LocalDateTime.now();
        System.out.println(supplier.get());
    }
}
Output:
2020-04-30T11:32:51.628

Supplier Interface Example #2

In this example, we will create a list of products and we will supply these products using the Supplier interface.
Let's create a simple Product POJO:
package com.javaguides.java.tutorial;

public class Product {
    private int id;
    private String name;
    private float price;

    public Product(int id, String name, float price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
    }
}
Example:
package com.javaguides.java.functionalinterfaces;

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

import com.javaguides.java.tutorial.Product;

public class SupplierDemo {

    public static void main(String[] args) {
        System.out.println(productSupplier().get());
    }

    private static Supplier < List < Product >> productSupplier() {
        Supplier < List < Product >> productSupplier = () - > {
            List < Product > productsList = new ArrayList < Product > ();
            productsList.add(new Product(1, "HP Laptop", 25000 f));
            productsList.add(new Product(2, "Dell Laptop", 30000 f));
            productsList.add(new Product(3, "Lenevo Laptop", 28000 f));
            productsList.add(new Product(4, "Sony Laptop", 28000 f));
            productsList.add(new Product(5, "Apple Laptop", 90000 f));
            productsList.add(new Product(6, "Apple Laptop", 90000 f));
            productsList.add(new Product(7, "Dell Laptop", 30000 f));
            productsList.add(new Product(8, "Dell Laptop", 30000 f));
            return productsList;
        };
        return productSupplier;
    }
}
Output:
[Product [id=1, name=HP Laptop, price=25000.0], Product [id=2, name=Dell Laptop, price=30000.0], Product [id=3, name=Lenevo Laptop, price=28000.0], Product [id=4, name=Sony Laptop, price=28000.0], Product [id=5, name=Apple Laptop, price=90000.0], Product [id=6, name=Apple Laptop, price=90000.0], Product [id=7, name=Dell Laptop, price=30000.0], Product [id=8, name=Dell Laptop, price=30000.0]]

Comments