Thursday, August 16, 2018

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 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 reference

Supplier interface declaration

@FunctionalInterface
public interface Supplier<T>{
  T get();
}
T is the result produced by this method.

Basic Supplier get Example

This is an example for usage of Supplier interface and calling 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 is
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 above code is
John

Supplier Static method call example

Supplier created with type using a direct static method using class and static method.
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 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 is
Ram

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



Related article


EmoticonEmoticon