Java 8 — Predicate, Consumer & Supplier Interface with Example
In this article we are about to discuss some of the predefined Functional Interfaces in Java 8.

If you are about to use Stream concepts in your Program, then you need to be aware of these three interfaces as they play a major role in the Stream building.
Let’s Start with Consumer, Followed by Predicate and Supplier.
What is Consumer Interface?
Consumer Interface is a Functional Interface, in which the object is being accepted as Single Input, it get’s processed but that returns no Output.
Here the functional method is void accept(T t).
This means the accept() method is accepting the T object which means type generic of any type it get’s processed but the return type is void.
public class ConsumerApp implements Consumer<Integer>{
@Override
public void accept(Integer t) {
System.out.println("Accept "+t);
}
public static void main(String[] args) throws Exception {
ConsumerApp consumer = new ConsumerApp();
consumer.accept(100);
}
}In the above part of code, we have implemented the Consumer Interface as it simply accepts the argument , processing it by printing it but not returning anything as output.
We are then Calling the consumer lambda expression using the Accept method.
public class ConsumerDemo {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
list1.stream().forEach(t -> System.out.println("print : " + t));
}
}Here forEach accepts the argument of type Consumer and thus print the data without returning anything.
What is Predicate Interface?
Predicate Interface is a Functional Interface, which is used for Conditional Checking.
Here the functional method is boolean test(T t).
Here the test() method is accepting the T object which is of type generic type and return the output in the form of True/False.
public class PredicateApp implements Predicate<Integer> {
@Override
public boolean test(Integer t) {
if( t % 2 == 0)return true;
else return false;
}
public static void main(String[] args) throws Exception {
PredicateApp predicate = new PredicateApp();
System.out.println(predicate.test(100));
}
}This is how normally the Predicate Interface is Implemented by creating the test method by passing the value to check the true or false condition.
public class PredicateDemo {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
list1.stream().filter(t -> t % 2 == 0).forEach(t -> System.out.println("print Even: " + t));
}
}Here, filter() method accepts the argument of type predicate functional Interface and returns predicate type.
What is Supplier Interface?
Supplier Interface is a Functional Interface, here no input is passed but that return the data.
Here the functional method is T get().
Here the get() method is not taking any arguments, but that is returning the Data of type generic.
public class SupplierApp implements Supplier<String>{
@Override
public String get() {
return "Supplier Funcyion Printing";
}
public static void main(String[] args) {
SupplierApp supplier = new SupplierApp();
System.out.println(supplier.get());
}
}Here we are calling the get() method, without passing any arguments, but in return it is returning the string value as output.
public class SupplierDemo {
public static void main(String[] args) {
List<String> list1 = Arrays.asList();
System.out.println(list1.stream().findAny().orElseGet(() -> "Hi viewers"));
}
}Here orElseGet() method will accept Supplier Interface as its input and print the Statement without taking anything as input.
Hope, this helps you to get some Idea about Predicate, Consumer and Supplier. Now you can start playing with them in your Stream Concepts.
Thanks for reading.Happy learning 😄




