Java 8 java.util.function.Supplier Tutorial with Examples
Introduction
Tutorial explains the in-built functional interface
Since Supplier is a functional interface, hence it can be used as the assignment target for a lambda expressionClick to read Lambda Expressions tutorial or a method referenceClick to read tutorial on Method References. Function Descriptor of
java.util.function.Supplier source code
Salient Points regarding
OUTPUT of the above code
Explanation of above example's Code & Output
Supplier<T>
introduced in Java 8. It explains with the help of examples how the Supplier interface is to be used via its get()
method.
What is java.util.function.Supplier
Supplier<T>
is an in-built functional interfaceClick to Read tutorial on Functional Interfaces introduced in Java 8 in the java.util.function
package. Supplier can be used in all contexts where there is no input but an output is expected.Since Supplier is a functional interface, hence it can be used as the assignment target for a lambda expressionClick to read Lambda Expressions tutorial or a method referenceClick to read tutorial on Method References. Function Descriptor of
Supplier<T>
Supplier's Function Descriptor is () -> T . This means that there is no input in the lambda definition and the return output is an object of type T. To understand Function Descriptors in details you can refer the function descriptor tutorialTutorial explaining function descriptors.
Advantage of predefined java.util.function.Supplier
In all scenarios where there is no input to an operation and it is expected to return an output the in-built functional interface Supplier<T>
can be used without the need to define a new functional interface every time.java.util.function.Supplier source code
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
* @return a result
*/
T get();
}
Supplier<T>’
s source code
Supplier
has been defined with the generic typeT
which is the same type which itsget()
methods return as output.get()
method is the primary abstract method of the Supplier functional interface. Its function descriptor being () -> T . I.e.get()
method takes no input and returns an output of type T. I will explain usage ofget()
with detailed example in the next section.- All lambda definitions for Supplier must be written in accordance with
get()
method's signature, and conversely all lambdas with the same signature as that ofget()
are candidates for assignment to an instance of Supplier interface.
get()
method of Supplier
To understand the get()
method lets take a look at the SupplierFunctionExample
’s code below, post which I have explained in detail how the code works - Code showing usage of Supplier.get() method
//SupplierFunctionExample.java
import java.util.Date;
import java.util.function.Supplier;
public class SupplierFunctionExample {
public static void main(String args[]) {
//Supplier instance with lambda expression
Supplier<String> helloStrSupplier = () -> new String("Hello");
String helloStr = helloStrSupplier.get();
System.out.println("String in helloStr is->"+helloStr+"<-");
//Supplier instance using method reference to default constructor
Supplier<String> emptyStrSupplier = String::new;
String emptyStr = emptyStrSupplier.get();
System.out.println("String in emptyStr is->"+emptyStr+"<-");
//Supplier instance using method reference to a static method
Supplier<Date> dateSupplier= SupplierFunctionExample::getSystemDate;
Date systemDate = dateSupplier.get();
System.out.println("systemDate->" + systemDate);
}
public static Date getSystemDate() {
return new Date();
}
}
String in helloStr is->Hello<-
String in emptyStr is-><-
systemDate->Wed Dec 16 19:18:15 IST 2015
SupplierFunctionExample
is my class with 2 methods -main()
&getSystemDate()
.getSystemDate()
is a static method which simply returns the current system date and does not take any input. The method signature matches the function descriptor of Supplier i.e. () -> T .- In
main()
method I have shown how to instantiate a Supplier interface instance in following 3 ways-- Using a Lambda Expression: I have defined a a lambda expression which takes no input and returns a new String object with value set to "hello". This lambda I have assigned to a
Supplier<String>
instance namedhelloStrSupplier
. Invoking functional methodget()
onhelloStrSupplier
gives us a StringhelloStr
which is then printed to show that it indeed contains the value "hello". - Using a Method Reference to default constructor of
String
: Method Reference to the default constructor ofString
is used to create aSupplier<String>
instance namedemptyStrSupplier
.emptyStrSupplier
is then used to create a String object namedemptyStr
using theget()
method.emptyStr
's value is then printed to show its value is empty as defined. - Using a Method Reference to
getSystemDate()
: Method Reference to thegetSystemDate()
method ofSupplierFunctionExample
class is used to create aSupplier<Date>
instance nameddateSupplier
.dateStrSupplier
is then used to create aDate
object namedsystemDate
by invokingget()
method on it.systemDate
's value is then printed to show its value. Value printed is of 16-Dec when I ran this example.
- Using a Lambda Expression: I have defined a a lambda expression which takes no input and returns a new String object with value set to "hello". This lambda I have assigned to a
Supplier<T>
in-built interface defined in Java 8 and what is its main advantage. We then looked at how to use the Supplier<T>
interface using its get()
method with an example.Functional Interface tutorials on JavaBrahman
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package
Functional Interfaces in Java 8Tutorial on basics of Functional Interfaces in Java 8 Predicate Interface TutorialClick to read tutorial on Predicate Functional Interface Consumer Interface TutorialClick to read tutorial on Consumer Functional Interface Function Interface TutorialClick to read tutorial on Function Interface Supplier Interface TutorialClick to read tutorial on Supplier Functional Interface java.util.function package overviewClick to read overview of java.util.function package