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