Java 8 Supplier Interface examples | Functional Interfaces tutorials in java

java.util.function.supplier Interface

The supplier is a functional interface defined in java.util.function package in java8. This returns the supplier of the result. It has a single abstract method get(). This interface is used as an assignment target for lambda expressions and method references/constructor references.

Following is a Supplier interface declaration

@FunctionalInterface
public interface Supplier<T>{
  T get();
}

T is the result produced by this method.

Simple Supplier get Example

It is an example of printing a message using the supplier. Created an object using the Supplier interface and called the get() method.

import java.util.function.Supplier;
public class MySupplierExample {
    public static void main(String[] args) {
        Supplier<String> stringSupplier = () -> new String("Hi Frank");
        String strMsg = stringSupplier.get();
        System.out.println(strMsg);
    }
}

and output:

Hi Frank

Supplier Object Creation of a class

The supplier is used to create an object of a class.

Class name and new keyword will create a supplier. supplier get method returns the object of a class.

import java.util.function.Supplier;
public class MySupplierExample {
    public static void main(String[] args) {
        Supplier<Message> messageSupplier = Message::new;
        Message message = messageSupplier.get();
        System.out.println(message.getMsg("John"));
    }
}
class Message {
    public String getMsg(String name) {
        return name;
    }
}

the output of the above code is

John

Supplier Static method call example

Another way is to create a Supplier object type using a direct static method with Class Name.

supplier.get() will be called to call static directly.

import java.util.function.Supplier;
public class MySupplierExample {
    public static void main(String[] args) {
        Supplier<String> strSupplier = Message::getStaticMsg;
        System.out.println(strSupplier.get());
    }
}
class Message {
    public static String getStaticMsg() {
        return "Static Message";
    }
}

The output of the above code is

Static Message

Passing Supplier as an argument Stream API methods example

Here supplier is passed as an argument to the stream method. Stream map() method accepts the supplier Functional interface as an argument.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class MySupplierExample {
    public static void main(String[] args) {
        List<User> users= new ArrayList<>();
        users.add(new User(1,"Kiran"));
        users.add(new User(2,"Frank"));
        users.add(new User(3,"John"));
        users.add(new User(4,"Tom"));
        Stream names = users.stream().map(User::getName);
        names.forEach(name -> System.out.println(name));
    }
}

public class User {
    Integer id;
    String name;
    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

The output of the above code

Kiran
Frank
John
Tom

Supplier Constructor arguments example

Created User object by passing Arguments and returns Supplier object. calling get() method return the object

import java.util.function.Supplier;
public class MySupplierExample {
 public static void main(String[] args) {
  Supplier<User> userSupplier= () -> new User(1,"hello");
  User user=userSupplier.get();
  System.out.println(user.getName());
 }
}

Output:

Ram

How to generate an infinite sequence of natural numbers

if you want to generate infinite natural numbers increment order, you can use the below function

import java.util.function.IntSupplier;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        IntSupplier naturalGenerator = new IntSupplier() {
            int currentValue = 0;

            public int getAsInt() {
                return currentValue++;
            }
        };

        IntStream naturalStream = IntStream.generate(naturalGenerator);

    }
}

Primitive Supplier interfaces

There are different Supplier interfaces for primitive types.

All the below interfaces has a single abstract method. All these interfaces are defined in java.util.function package

  • IntSupplier - has getAsInt() method
  • LongSupplier - has getAsLong() method
  • DoubleSupplier - has getAsDouble() method
  • BooleanSupplier - has getAsBoolean() method

Conclusion

In this tutorial, Learned Supplier functional interface with various examples using this class