#
Suppliers in Java
A Functional Interface
is an interface which contains exactly one abstract method
.
It can have any number of default, static methods, but it can contain ONLY one abstract method.
Lambda expression is used to implement a functional interface abstract method.
Functional Interfaces and lambda expression are introduced in Java 8.
The Suppliers are implementations of the Supplier<T>
functional interfaces.
In my example, I start from a simple Spring Boot application created using Spring initializr.
For my example I create an Employee class first:
package com.exampe.java;
public class Employee {
int id;
String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
After that we can create a Supplier<Employee>
implementation:
package com.exampe.java;
import java.util.function.Supplier;
public class Supplier1 implements Supplier<Employee> {
Employee emp = new Employee(11, "Emily");
@Override
public Employee get() {
return emp;
}
}
And below we can see how the suppliers are used:
package com.exampe.java;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.function.Consumer;
import java.util.function.Supplier;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("--------------------------------------------");
// Supplier using a class
Supplier1 supplier1 = new Supplier1();
Employee emp1 = supplier1.get();
System.out.println("Emp name (I) = "+ emp1.getName());
// Supplier using lambda expression
Supplier<Employee> supplier2 = () -> {
Employee emp_2 = new Employee(22, "Dan");
return emp_2;
};
Employee emp2 = supplier2.get();
System.out.println("Emp name (II) = "+ emp2.getName());
// Supplier using an inner class
Supplier<Employee> supplier3 = new Supplier<Employee>() {
public Employee get() {
Employee emp_3 = new Employee(33, "John");
return emp_3;
}
};
Employee emp3 = supplier3.get();
System.out.println("Emp name (III) = "+ emp3.getName());
System.out.println("--------------------------------------------");
System.out.println("End of the execution");
}
}
When we run the application we can see:
--------------------------------------------
Emp name (I) = Emily
Emp name (II) = Dan
Emp name (III) = John
--------------------------------------------
End of the execution
Here are some remarks:
- "lambda expressions" could be used always with a functional interface;
- the suppliers could be implemented by an inner or a normal class.
- a Supplier implements the get() method.
There are a couple of predefined functional interfaces:
Predicate<T>
orBiPredicates<T,U>
: used for filters, we need to define test(T) or test(T, U) method. This method returns a boolean value.Consumer<T>
orBiConsumer<T,U>
: we need to define accept(T) or accept(T, U) method. This method returns no value.Supplier<T>
: we need to return theobject . The get() method will be used to obtain the object returned by the supplier.Function<T, R>
orBiFunction<T,U,R>
: we need to define apply(T) or apply(T, U) method. This method returns an R object.UnaryOperator<T>
orBinaryOperator<T,T>
: we need to return a T object.
Info
T, U, R are generic types of objects.