Predicate Consumer Supplier Java 8Java 8 introduced several functional programming features to simplify code and make it more expressive. Among these features are Predicate, Consumer, and Supplier interfaces, which provide powerful tools for working with collections, filtering data, and more. In this section, we will dive into these three interfaces, explore their use cases, and provide code examples to illustrate their functionality. 1. PredicateThe Predicate interface represents a single argument function that returns a boolean value. It is commonly used for filtering elements in a collection or making decisions based on conditions. Here's the basic structure of a Predicate: The Predicate interface is a functional interface, which means it has only one abstract method: Here, T represents the type of the input object, and the test method takes an object of type T and returns a boolean value. Example: Filtering a List Let's say we have a list of integers, and we want to filter out all even numbers using a Predicate: PredicateExample.java Output: Even numbers: [2, 4, 6, 8, 10] Explanation In this example, we define a Predicate using a lambda expression, which checks if a number is even (i.e., the remainder when divided by 2 is 0). We then use this predicate to filter the list, resulting in a new list containing only the even numbers. 2. ConsumerThe Consumer interface represents an operation that accepts a single input and returns no result. It is often used for performing actions on elements of a collection or processing data without returning anything. Here's the basic structure of a Consumer: Here, T represents the type of the input object, and the accept method takes an object of type T and performs some action on it without returning any value. Example: Printing Elements Suppose we have a list of strings, and we want to print each element using a Consumer: ConsumerExample.java Output: Hello, Alice! Hello, Bob! Hello, Charlie! Hello, David! Explanation In this example, we define a Consumer using a lambda expression that prints each name. We then use the forEach() method to apply the consumer to each element in the list. 3. SupplierThe Supplier interface represents a supplier of results, producing an object without any input. It is commonly used for lazy initialization or generating data on demand. Here is the basic structure of a Supplier: Here, T represents the type of the object to be supplied, and the get method generates and returns an object of type T. Example: Generating Random Numbers Let's create a Supplier to generate random integers: SupplierExample.java Output: Random Number: 55 Random Number: 59 Random Number: 77 Random Number: 79 Random Number: 64 Explanation In this example, we define a Supplier using a lambda expression that generates random integers between 0 and 99 each time get() is called. We then use the get() method to obtain and print random numbers. ConclusionIn Java 8, the Predicate, Consumer, and Supplier interfaces provide essential tools for functional programming. Predicate is used for filtering and making decisions, Consumer for performing actions on elements, and Supplier for lazy initialization or generating data. These interfaces, along with lambda expressions, make Java code more concise and expressive, allowing developers to write cleaner and more maintainable code. By understanding and effectively using these functional interfaces, we can leverage the power of Java 8 and improve your code's readability and flexibility. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India