Supplier is functional interface which does not take any argument and produces result of
type T
.It has a functional method called T get()
As Supplier is functional interface, so it can be used as assignment target for lambda expressions.Table of Contents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package java.util.function; /** * Represents a supplier of results. * * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #get()}. * * @param <T> the type of results supplied by this supplier * * @since 1.8 */ @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } |
Java 8 Supplier example
Lets use supplier
interface to print String:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.function.Supplier; public class Java8SupplierExample { public static void main(String[] args) { Supplier<String> supplier= ()-> "Arpit"; System.out.println(supplier.get()); } } |
It is simple use of supplier interface
to get String
object. When you run above program, you will get below output:
Let’s create another example using custom class Student
.We will use supplier to supply new Student
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package org.arpit.java2blog; public class Student { private int id; private String name; private String gender; private int age; public Student(int id, String name, String gender, int age) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]"; } } |
Now lets create Supplier
object which will be used to supply new Student
object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.code2master; import java.util.function.Supplier; public class Java8SupplierExample { public static void main(String[] args) { Supplier studentSupplier = () -> new Student(1, "Arpit", "M", 19); Student student = studentSupplier.get(); System.out.println(student); } } |
When you run above program, you will get below output:
Use of supplier in Stream’s generate method
If you observe signature of Stream’s generate method, you will notice that it takes supplier as argument.
1 2 3 |
public static<T> Stream<T> generate(Supplier<T> s) |
Stream's generate method
returns an infinite sequential stream
where supplier generates each element.
Let’s say you want to generate 5 random numbers between 0 to 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.util.Random; import java.util.function.Supplier; import java.util.stream.Stream; public class Java8SupplierExample { public static void main(String[] args) { Supplier<Integer> randomNumbersSupp=() -> new Random().nextInt(10); Stream.generate(randomNumbersSupp) .limit(5) .forEach(System.out::println); } } |
Output:
3
5
2
2
As you want see we have used supplier
as () -> new Random().nextInt(10)
to generate random numbers. limit(5)
is used to limit the stream to 5 elements only and also used Java 8 foreach loop to print elements of the Stream.
That’s all about Java 8 Supplier example.
nice