Introduction
In this blog we will learn how to use Supplier functional interface in java.util.Function
.
Supplier only supplies values.
It does not take any value for processing. It just supplies values internally.
Just we just can supply random number, constants or objects using our own business logic.
Supplier<T>
only returns value.
Supplier<T>
functional method is T get()
.
The ‘T’ in the Supplier<T>
represents type of result that the get() method will return.
In the below example we can see Supplier interface does not accept anything but only return random values.
Basic usage example:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.examples;
import java.util.Random;
import java.util.function.Supplier;
public class App {
public static void main(String[] args) {
Supplier<Integer> nums = () -> new Random().nextInt(1,100);
System.out.println(nums.get()); // 81
}
}
|
Constant value supplier
In the below example Supplier only supplies constant value.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.examples;
import java.util.function.Supplier;
public class App {
public static void main(String[] args) {
Supplier<String> data = () -> "APP_NAME=TEST INC";
System.out.println(data.get()); // APP_NAME=TEST INC
}
}
|
Supplying objects
In the below example Supplier supplies list of objects like employees.
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
|
package com.examples;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class App {
public static void main(String[] args) {
Supplier<List<Employee>> employees = () -> {
List<Employee> emps = new ArrayList<>();
emps.add(new Employee(1,"Tim",1000));
emps.add(new Employee(2,"Jim",2000));
emps.add(new Employee(3,"Harish",3000));
return emps;
};
System.out.println(employees.get());
}
}
class Employee {
private int empId;
private String empName;
private int salary;
public Employee(int empId, String empName, int salary) {
this.empId = empId;
this.empName = empName;
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", salary=" + salary +
'}';
}
}
|
1
2
3
4
|
Output:
[Employee{empId=1, empName='Tim', salary=1000}, Employee{empId=2, empName='Jim', salary=2000},
Employee{empId=3, empName='Harish', salary=3000}]
|
Supplying values for Streams
Supplier can be used to provide random values for Stream operations like generate()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.examples;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class App {
public static void main(String[] args) {
Supplier<Integer> data = () -> new Random().nextInt(1,100);
Stream.generate(data)
.limit(5)
.forEach(System.out::println);
}
}
|
Just we can see we can use Supplier functional interface for getting random values, constants, objects and supplying values for streams.
Conclusion
I hope this tutorial has helped you to get started with Supplier Interface
in Java.
Learn more functional interfaces
- Function interface
- Consumer interface
- Predicate interface
- BiPredicate interface
- BiFunction interface