Supplier in java8 with code example

Introduction to Supplier:

A Supplier<T> is a functional interface with only one abstract method called get() which returns an object of type T.They do not expect any inputs.Supplier in java8 is usually used to get a new object or an existing object(Factory pattern) and also to create streams.

Java doc

Interface Supplier<T>
Type Parameters:
T - the type of results supplied by this supplier
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Supplier Code Example 1:

In the below code example, I have created a Supplier to get current time in a specified format. I am invoking the get method of the supplier to get current time.This example of a stateless supplier.Meaning that the next output is not dependent on any of its previous outputs or inputs.But Supplier’s can be stateful too but they are not safe to use in multi threaded code’s.

package com.codingbrains.examples;


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

public class SupplierEx {

  public static void main(String[] args) {
    Supplier<String> systemTime=()->new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
    System.out.println("Current time is "+systemTime.get());

  }
}

Code Output:

Current time is 2020.05.12.07.43.57

Code Example 2:

Lets take a look at another example for Supplier. Below is a Car class with different properties called name,model,color,brand and weight. Ill create a Supplier in such a way that it returns a new Car object every time I invoke it.

package com.model;

public class Car {

  private String name;
  private String model;
  private String color;
  private String Brand;
  private int weight;

  public Car(String name, String model, String color, int weight) {
    setName(name);
    setModel(model);
    setColor(color);
    setWeight(weight);
  }

  public String getName() {
    return name;
  }

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

  public String getModel() {
    return model;
  }

  public void setModel(String model) {
    this.model = model;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public String getBrand() {
    return Brand;
  }

  public void setBrand(String brand) {
    Brand = brand;
  }

  public int getWeight() {
    return weight;
  }

  public void setWeight(int weight) {
    this.weight = weight;
  }
}

I am invoking get() method on Supplier fiatSupplier for the car object.

package com.codingbrains.examples;

import com.model.Car;
import java.util.function.Supplier;

public class CarSupplier {

  public static void main(String[] args) {
    Supplier<Car> fiatSupplier=()->new Car("Punto","2018","White",500);

    System.out.println("Punto car from supplier of "+fiatSupplier.get().getWeight()+" weight");
  }
}

Code Output:

Punto car from supplier of 500 weight

Primitive Variants of Supplier’s:

Below are the primitive variants of Supplier’s in Java8 They are:

  1. BooleanSupplier with an abstract method ‘getAsBoolean()’.
  2. IntSupplier with an abstract method ‘getAsInt()’
  3. LongSupplier with an abstract method ‘getAsLong()’.
  4. DoubleSupplier with an abstract method ‘getAsDouble()’

You may also like...

Leave a Reply

Your email address will not be published.