GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java LongPredicate Interface - Complete Tutorial with Examples Java LongPredicate InterfaceLast modified: April 16, 2025
The
LongPredicate Interface Overview
@FunctionalInterface
public interface LongPredicate {
boolean test(long value);
default LongPredicate and(LongPredicate other);
default LongPredicate or(LongPredicate other);
default LongPredicate negate();
}
The code above shows the structure of Basic LongPredicate UsageThe simplest way to use LongPredicate is with lambda expressions. We define the condition to test against long values. The example checks if numbers are even. Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
// Check if number is even
LongPredicate isEven = n -> n % 2 == 0;
System.out.println("Is 10 even? " + isEven.test(10));
System.out.println("Is 15 even? " + isEven.test(15));
// Check if number is positive
LongPredicate isPositive = n -> n > 0;
System.out.println("Is -5 positive? " + isPositive.test(-5));
}
}
This example demonstrates basic LongPredicate usage with lambda expressions. We create two predicates: one checks for even numbers, another for positive numbers. The test method evaluates each predicate with different values. Combining Predicates with AND
The Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
LongPredicate isEven = n -> n % 2 == 0;
LongPredicate isGreaterThanTen = n -> n > 10;
// Combine predicates with AND
LongPredicate isEvenAndGreaterThanTen = isEven.and(isGreaterThanTen);
System.out.println("12: " + isEvenAndGreaterThanTen.test(12));
System.out.println("8: " + isEvenAndGreaterThanTen.test(8));
System.out.println("15: " + isEvenAndGreaterThanTen.test(15));
}
}
This example shows predicate combination with Combining Predicates with OR
The Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
LongPredicate isNegative = n -> n < 0;
LongPredicate isGreaterThanHundred = n -> n > 100;
// Combine predicates with OR
LongPredicate isOutOfRange = isNegative.or(isGreaterThanHundred);
System.out.println("-5: " + isOutOfRange.test(-5));
System.out.println("50: " + isOutOfRange.test(50));
System.out.println("150: " + isOutOfRange.test(150));
}
}
This example demonstrates predicate combination with Negating a Predicate
The Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
LongPredicate isPrime = n -> {
if (n < 2) return false;
for (long i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
};
// Create negation of isPrime
LongPredicate isNotPrime = isPrime.negate();
System.out.println("7 is prime? " + isPrime.test(7));
System.out.println("8 is not prime? " + isNotPrime.test(8));
}
}
This example shows predicate negation with Using LongPredicate with StreamsLongPredicate is commonly used with LongStream for filtering operations. The filter method accepts a LongPredicate to include elements that match the condition. This enables efficient processing of long values. Main.java
package com.zetcode;
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
// Define a range of numbers
LongStream numbers = LongStream.rangeClosed(1, 20);
// Filter for numbers divisible by 3 or 5
LongPredicate divisibleBy3or5 = n -> n % 3 == 0 || n % 5 == 0;
System.out.println("Numbers divisible by 3 or 5:");
numbers.filter(divisibleBy3or5)
.forEach(System.out::println);
}
}
This example demonstrates LongPredicate usage with LongStream. We create a predicate to check divisibility by 3 or 5, then use it to filter a range of numbers. The filtered stream only contains matching values. Chaining Multiple PredicatesLongPredicate's default methods can be chained to create complex conditions. This allows building sophisticated predicates from simple components while maintaining readability. Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
LongPredicate isEven = n -> n % 2 == 0;
LongPredicate isPositive = n -> n > 0;
LongPredicate isTwoDigits = n -> n >= 10 && n <= 99;
// Chain multiple predicates
LongPredicate complexCondition = isPositive.and(isTwoDigits).and(isEven.negate());
System.out.println("25: " + complexCondition.test(25));
System.out.println("-5: " + complexCondition.test(-5));
System.out.println("12: " + complexCondition.test(12));
System.out.println("99: " + complexCondition.test(99));
}
}
This example shows chaining multiple predicates. We check for positive, two-digit, odd numbers. The complex condition is built by combining simpler predicates with and and negate operations. Practical Example: Number ValidatorLet's create a practical number validator using LongPredicate. We'll combine several validation rules to check if numbers meet specific business requirements. Main.java
package com.zetcode;
import java.util.function.LongPredicate;
public class Main {
public static void main(String[] args) {
// Define validation rules
LongPredicate isInAllowedRange = n -> n >= 1000 && n <= 9999;
LongPredicate isNotForbiddenNumber = n -> n != 1234 && n != 4321;
LongPredicate hasValidChecksum = n -> (n % 10 + n / 10 % 10) % 2 == 0;
// Combine all rules
LongPredicate isValidNumber = isInAllowedRange
.and(isNotForbiddenNumber)
.and(hasValidChecksum);
long[] testNumbers = {1234, 5678, 9999, 4321, 2468, 1001};
for (long num : testNumbers) {
System.out.printf("%d is valid: %b%n", num, isValidNumber.test(num));
}
}
}
This practical example creates a number validator with multiple rules. Numbers must be 4-digit, not forbidden, and pass a checksum test. The combined predicate efficiently checks all conditions in one operation. SourceJava LongPredicate Interface Documentation In this article, we've covered the essential methods and features of the Java LongPredicate interface. Understanding these concepts is crucial for functional programming and stream processing with primitive longs in Java applications. AuthorMy name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts. List all Java tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |