Predicate Functional Interface in Java 8 – Definition, Syntax & Real-World Use Cases
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
package com.rkdigital.school;
public interface Predicate<T>
{
boolean test(T t);
}
Program1: write a program to check the given String is length is greater than 6 or not
package com.rkdigital.school.predefinefun;
import java.util.function.Predicate;
public class PredicateTest {
public static void main(String[] args) {
Predicate<String> p=s->s.length()>10;
System.out.println(p.test("Rk Digital School"));
System.out.println(p.test("Founder"));
System.out.println(p.test("Rakesh Kumar"));
}
}
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.
package com.rkdigital.school.predefinefun;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;
public class ProgramTest {
public static void main(String[] args) {
Predicate<Collection> p = c->c.isEmpty();
ArrayList list1=new ArrayList();
list1.add("Rk digital school");
list1.add("Rakesh Kumar");
list1.add("Founder of Rk digital school");
System.out.println(p.test(list1));
}
}
Program3: write a program to filter the odd number using predicate .
package com.rkdigital.school.predefinefun;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class PredicateExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,12,45,33,44,55,76);
Predicate<Integer> isEven=num->num%2==0;
List<Integer> evenNUmber=numbers.stream().filter(isEven).collect(Collectors.toList());
System.out.println(evenNUmber);
}
}
Program4: write a program to display names start with ‘R’ By using predicate .
package com.rkdigital.school.predefinefun;
import java.util.function.Predicate;
public class PredicateNames {
public static void main(String[] args) {
String[] names={"Raksh","Sanjana","Rahul Gupta","Rahul Singh"};
Predicate<String> starWithR=s->s.charAt(0)=='R';
for (String string : names) {
if (starWithR.test(string)) {
System.out.println(string);
}
}
}
}
Consumer
Represents an action that uses a value but does not return anything , used for performing operations on input data without returning anything.
package com.rkdigital.school;
import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer;
public class TestMain {
public static void main(String[] args) {
Consumer<String> c=s->System.out.println(s);
c.accept("Hello Rakesh");
c.accept("How are you");
}
}
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 :
Supplier<T> supplier = () -> {
return value;
};