avatarAmar Balu

Summary

The provided web content discusses the functional interfaces Predicate, Consumer, and Supplier in Java 8, explaining their purpose, method signatures, and usage with examples.

Abstract

The article "Java 8 — Predicate, Consumer & Supplier Interface with Example" delves into three essential functional interfaces introduced in Java 8: Predicate, Consumer, and Supplier. It explains that these interfaces are integral to using Stream concepts effectively. The Consumer interface accepts a single input, processes it, and returns no result, exemplified by the accept method. The Predicate interface is used for conditional checking and returns a boolean value through its test method. Lastly, the Supplier interface provides a value without taking any input, utilizing the get method. The article provides code snippets to illustrate practical implementations of these interfaces, demonstrating their application in lambda expressions and Stream operations such as forEach, filter, and findAny. The author aims to equip readers with a foundational understanding of these interfaces to facilitate their use in Stream processing.

Opinions

  • The author emphasizes the importance of understanding Predicate, Consumer, and Supplier interfaces for developers who intend to use Stream concepts in their Java 8 programs.
  • The article suggests that mastery of these functional interfaces is crucial for effective Stream building and processing.
  • The use of practical examples and code snippets indicates the author's belief in learning through application and demonstration.
  • The author's inclusion of both standalone implementations and integration within Stream operations implies a comprehensive approach to teaching these concepts.
  • The concluding remarks encourage readers to experiment with these interfaces, indicating the author's support for hands-on learning and exploration.

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.

Image Source : https://javadeveloperzone.com/spring-boot/spring-boot-tutorial/

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 😄

Java
Java8
Interfaces
Spring
Stream
Recommended from ReadMedium