In this tutorial, we will learn how to use Predicate functional interface with an example.
java.util.function.Predicate is a functional interface that can be used as an assignment target for a lambda expression. The Predicate interface represents an operation that takes a single input and returns a boolean value.
check out functional interfaces tutorial at https://www.javaguides.net/2018/07/java-8-functional-interfaces.html
Check out Java 8 tutorial at https://www.javaguides.net/p/java-8.html
YouTube Video
Predicate Implementation Code
This is the internal implementation of the Predicate interface:
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
}
T – Type of the input to the predicate
Predicate Interface Example #1
The following example shows how to use the test() method of the Predicate interface using lambda.
package com.javaguides.java.functionalinterfaces;
import java.util.function.Predicate;
public class PredicateDemo {
public static void main(String[] args) {
Predicate < Integer > predicate = (t) -> {
if (t % 2 == 0) {
return true;
} else {
return false;
}
};
System.out.println(predicate.test(10));
}
}
Output:
true
Predicate.and() Method Example #2
This example demonstrates how to use Predicate.and() with filter() method:
package com.javaguides.java.functionalinterfaces;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateDemo {
public static void main(String[] args) {
// example 2
List < Integer > list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Predicate < Integer > predicate2 = (x) -> x > 5;
Predicate < Integer > predicate3 = (x) -> x < 8;
list1.stream().filter(predicate2.and(predicate3)).collect(Collectors.toList()).forEach(System.out::println);
}
}
Output:
6
7
Predicate.or() Method Example #3
This example demonstrates how to use Predicate.or() with filter() method:
package com.javaguides.java.functionalinterfaces;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateDemo {
public static void main(String[] args) {
// example 4
Predicate < String > lengthIs3 = x -> x.length() == 3;
Predicate < String > startWithA = x -> x.startsWith("A");
List < String > list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
List < String > collect = list.stream()
.filter(lengthIs3.or(startWithA))
.collect(Collectors.toList());
System.out.println(collect);
}
}
Output:
[A, AA, AAA, BBB]
check out functional interfaces tutorial at https://www.javaguides.net/2018/07/java-8-functional-interfaces.html
Check out Java 8 tutorial at https://www.javaguides.net/p/java-8.html
Predicate Interface Example #4
In this example, we will check if a person's age is greater than 30 using Predicate.test() method and return the result in boolean value:
package com.java.tutorials.functionalinterfaces;
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate < Person > predicate = (person) -> person.getAge() > 30;
boolean result = predicate.test(new Person("Ramesh", 31));
System.out.println(result);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Output:
true
Comments
Post a Comment
Leave Comment