🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, the Predicate interface is a functional interface that represents a predicate (boolean-valued function) of one argument. It is part of the java.util.function package and is commonly used for evaluating conditions and filtering data.
Table of Contents
- What is
Predicate? - Methods and Syntax
- Examples of
Predicate - Real-World Use Case
- Conclusion
1. What is Predicate?
Predicate is a functional interface that accepts one argument and returns a boolean result. It is useful for scenarios where a condition needs to be checked or data needs to be filtered based on a criterion.
2. Methods and Syntax
The main methods in the Predicate interface are:
boolean test(T t): Evaluates this predicate on the given argument.default Predicate<T> and(Predicate<? super T> other): Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.default Predicate<T> or(Predicate<? super T> other): Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.default Predicate<T> negate(): Returns a predicate that represents the logical negation of this predicate.
Syntax
Predicate<T> predicate = (T t) -> {
// condition on t
return result;
};
3. Examples of Predicate
Example 1: Checking if a String is Empty
import java.util.function.Predicate;
public class IsEmptyExample {
public static void main(String[] args) {
// Define a Predicate that checks if a string is empty
Predicate<String> isEmpty = (str) -> str.isEmpty();
boolean result = isEmpty.test("");
System.out.println("Is empty? " + result);
}
}
Output:
Is empty? true
Example 2: Using and
import java.util.function.Predicate;
public class AndExample {
public static void main(String[] args) {
// Define predicates for checking if a string is not empty and has more than 5 characters
Predicate<String> isNotEmpty = (str) -> !str.isEmpty();
Predicate<String> hasMoreThanFiveChars = (str) -> str.length() > 5;
Predicate<String> isValid = isNotEmpty.and(hasMoreThanFiveChars);
boolean result = isValid.test("HelloWorld");
System.out.println("Is valid? " + result);
}
}
Output:
Is valid? true
Example 3: Using or
import java.util.function.Predicate;
public class OrExample {
public static void main(String[] args) {
// Define predicates for checking if a string is empty or has more than 5 characters
Predicate<String> isEmpty = (str) -> str.isEmpty();
Predicate<String> hasMoreThanFiveChars = (str) -> str.length() > 5;
Predicate<String> isEither = isEmpty.or(hasMoreThanFiveChars);
boolean result = isEither.test("Hello");
System.out.println("Is either? " + result);
}
}
Output:
Is either? false
Example 4: Using negate
import java.util.function.Predicate;
public class NegateExample {
public static void main(String[] args) {
// Define a predicate for checking if a string is empty
Predicate<String> isEmpty = (str) -> str.isEmpty();
Predicate<String> isNotEmpty = isEmpty.negate();
boolean result = isNotEmpty.test("Hello");
System.out.println("Is not empty? " + result);
}
}
Output:
Is not empty? true
4. Real-World Use Case: Filtering a List of Strings
In applications, Predicate can be used to filter a list of strings based on certain criteria.
import java.util.function.Predicate;
import java.util.List;
import java.util.stream.Collectors;
public class FilterStrings {
public static void main(String[] args) {
List<String> strings = List.of("Apple", "Banana", "Cherry", "Date", "Fig");
// Define a Predicate to filter strings that start with 'B'
Predicate<String> startsWithB = (str) -> str.startsWith("B");
List<String> filteredStrings = strings.stream()
.filter(startsWithB)
.collect(Collectors.toList());
System.out.println("Filtered strings: " + filteredStrings);
}
}
Output:
Filtered strings: [Banana]
Conclusion
The Predicate interface is used in Java for evaluating conditions involving objects. It simplifies filtering and logical operations, enhancing code clarity and maintainability. Using Predicate can lead to cleaner and more efficient code, especially in functional programming contexts.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment