Predicate in Java 8
In Java, a predicate is a functional interface introduced in the Java 8
A predicate represents a boolean-valued function that takes an argument and returns true or false.
The Predicate interface has a single abstract method called test, which evaluates the predicate.
Here’s the basic structure of the Predicate interface:
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}Here, T is the type of input to the predicate. You can use the test() method to define the condition you want to check.
Get Java Fusion’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s look at a simple example. Suppose you have a list of integers, and you want to filter out the even numbers using a predicate
package com.java8features.predicate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateDemo {
public void predicateExample() {
System.out.println("*** filter out the even numbers using a predicate**");
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5,6,7,8);
Predicate<Integer> predicate = n -> n % 2 == 0;
for (Integer val : list) {
if (predicate.test(val)) { // test method called to cecheck condition
System.out.println(val);
}
}
String[] std = {"runa", "muna", "kuna", "kanha", "miku"};
Predicate<String> names = (name) -> name.startsWith("u", 1);
for (String s : std) {
if (names.test(s)) { // test method called to cecheck condition
System.out.println(s);
}
}
System.out.println("*** filter out the odd numbers using a predicate**");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenlist = numbers.stream().filter(val -> val % 2 != 0).toList();
for (Integer val : evenlist) {
System.out.println(val);
}
}
public static void main(String[] args) {
System.out.println("This is predicate example");
PredicateDemo predicateDemo = new PredicateDemo();
predicateDemo.predicateExample();
}
}This is a predicate example with a stream API filter
package com.java8features.predicate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateDemo {
public void predicateExample() {
Predicate<Integer> isEven = n -> n % 2 == 0;
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = filterList(numbers, isEven);
System.out.println(evenNumbers);
}
private static List<Integer> filterList(List<Integer> numbers, Predicate<Integer> predicate) {
return numbers.stream().filter(predicate).toList();
}
public static void main(String[] args) {
System.out.println("This is predicate example with stream API filter");
PredicateDemo predicateDemo = new PredicateDemo();
predicateDemo.predicateExample();
}
}In this example, the isEvenpredicate checks whether a given integer is even or not. The filterList method takes a list and a predicate, and it returns a new list containing only the elements that satisfy the condition specified by the predicate.
Output Filtered list of even numbers
Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Even Numbers: [2, 4, 6, 8, 10]Thank you for taking the time to read our blog post! If you found the information helpful or interesting, don’t forget to show your appreciation by giving it a round of applause 👏.
Happy reading and exploring! 🚀✨ Thank you !!










