stock market

Predicate Functional Interface in Java 8 – Definition, Syntax & Real-World Use Cases

Spread the love

Java 8 introduced the concept of functional programming in Java through Lambda Expressions.
To support this, Java provides a rich set of predefined functional interfaces .

An Interface which contains single abstract method and any number of default and statics method .all predefined functional interface present in java.util.function package.

Main Types of Predefined Functional Interfaces

  • Predicate
  • Function
  • Consumer
  • Supplier

1.Predicate :

A predicate is a functional interface that represents a Boolean valued function of one argument . predicate takes one input and return either true or false .

the Predicate functional interface is mainly used to represent business conditions or validation rules that return a boolean result and are frequently applied in filtering operations. A common real-time use case is filtering data in applications such as CRM systems, banking platforms, or e-commerce portals, where you may need to select only active users, approved transactions, or valid records from a collection

Program1: write a program to check the given String is length is greater than 6 or not

Program2: Write program to check the given list is empty or not .

In real-world applications, it is very common to validate data before processing it. For example, while fetching user records from a database, reading API responses, or processing uploaded files, developers must first check whether the received list contains data or not. Performing operations on an empty list can lead to logical errors, unnecessary processing, or runtime issues. Therefore, checking whether a list is empty ensures safe execution of business logic and improves application reliability.

Program3: write a program to filter the odd number using predicate .

Program4: write a program to display names start with ‘R’ By using predicate .

Consumer

Represents an action that uses a value but does not return anything , used for performing operations on input data without returning anything.

Supplier

Supplier<T> is a functional interface that supplies (provides) an object of type T.
It does not take any input, but returns a result. used for supplying or generating values without taking any input.

Syntax :

Leave a Reply

Your email address will not be published. Required fields are marked *