🎓 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 IntPredicate interface is a functional interface that represents a predicate (boolean-valued function) with a single int-valued argument. It is part of the java.util.function package and is used for testing conditions involving int values.
Table of Contents
- What is
IntPredicate? - Methods and Syntax
- Examples of
IntPredicate - Real-World Use Case
- Conclusion
1. What is IntPredicate?
IntPredicate is a functional interface that accepts an int and returns a boolean result. It is commonly used for evaluating conditions or filtering data.
2. Methods and Syntax
The main methods in the IntPredicate interface are:
boolean test(int value): Evaluates this predicate on the given argument.default IntPredicate and(IntPredicate other): Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.default IntPredicate or(IntPredicate other): Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.default IntPredicate negate(): Returns a predicate that represents the logical negation of this predicate.
Syntax
IntPredicate intPredicate = (int value) -> {
// condition on value
return result;
};
3. Examples of IntPredicate
Example 1: Checking if a Number is Even
import java.util.function.IntPredicate;
public class EvenCheckExample {
public static void main(String[] args) {
// Define an IntPredicate that checks if a number is even
IntPredicate isEven = (value) -> value % 2 == 0;
boolean result = isEven.test(10);
System.out.println("Is 10 even? " + result);
}
}
Output:
Is 10 even? true
Example 2: Using and
import java.util.function.IntPredicate;
public class AndExample {
public static void main(String[] args) {
// Define predicates for checking if a number is even and greater than 5
IntPredicate isEven = (value) -> value % 2 == 0;
IntPredicate isGreaterThanFive = (value) -> value > 5;
IntPredicate isEvenAndGreaterThanFive = isEven.and(isGreaterThanFive);
boolean result = isEvenAndGreaterThanFive.test(8);
System.out.println("Is 8 even and greater than 5? " + result);
}
}
Output:
Is 8 even and greater than 5? true
Example 3: Using or
import java.util.function.IntPredicate;
public class OrExample {
public static void main(String[] args) {
// Define predicates for checking if a number is even or greater than 5
IntPredicate isEven = (value) -> value % 2 == 0;
IntPredicate isGreaterThanFive = (value) -> value > 5;
IntPredicate isEvenOrGreaterThanFive = isEven.or(isGreaterThanFive);
boolean result = isEvenOrGreaterThanFive.test(3);
System.out.println("Is 3 even or greater than 5? " + result);
}
}
Output:
Is 3 even or greater than 5? false
Example 4: Using negate
import java.util.function.IntPredicate;
public class NegateExample {
public static void main(String[] args) {
// Define a predicate for checking if a number is even
IntPredicate isEven = (value) -> value % 2 == 0;
IntPredicate isOdd = isEven.negate();
boolean result = isOdd.test(5);
System.out.println("Is 5 odd? " + result);
}
}
Output:
Is 5 odd? true
4. Real-World Use Case: Filtering Even Numbers from a List
In applications, IntPredicate can be used to filter even numbers from a list.
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
public class FilterEvenNumbers {
public static void main(String[] args) {
// Define an IntPredicate to check for even numbers
IntPredicate isEven = (value) -> value % 2 == 0;
IntStream.of(1, 2, 3, 4, 5, 6)
.filter(isEven)
.forEach(value -> System.out.println("Even number: " + value));
}
}
Output:
Even number: 2
Even number: 4
Even number: 6
Conclusion
The IntPredicate interface is used in Java for evaluating conditions involving int values. It simplifies filtering and logical operations, enhancing code clarity and maintainability. Using IntPredicate 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