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 DoublePredicate Interface - Complete Tutorial with Examples Java DoublePredicate InterfaceLast modified: April 16, 2025
The
DoublePredicate Interface Overview
@FunctionalInterface
public interface DoublePredicate {
boolean test(double value);
default DoublePredicate and(DoublePredicate other);
default DoublePredicate negate();
default DoublePredicate or(DoublePredicate other);
}
The code above shows the structure of Basic DoublePredicate UsageThe simplest way to use DoublePredicate is with lambda expressions. We define the condition to test against double values. The example checks if numbers are positive. Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Define a predicate that tests if a number is positive
DoublePredicate isPositive = n -> n > 0;
// Test the predicate
System.out.println("5.0 is positive: " + isPositive.test(5.0));
System.out.println("-3.2 is positive: " + isPositive.test(-3.2));
System.out.println("0.0 is positive: " + isPositive.test(0.0));
}
}
This example demonstrates basic DoublePredicate usage with lambda expression. The isPositive predicate tests if a number is greater than zero. We apply it to different values to see the boolean results. Combining Predicates with AND
The Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Define individual predicates
DoublePredicate isPositive = n -> n > 0;
DoublePredicate isWholeNumber = n -> n == Math.floor(n);
// Combine with AND
DoublePredicate isPositiveWhole = isPositive.and(isWholeNumber);
System.out.println("5.0 is positive whole: " + isPositiveWhole.test(5.0));
System.out.println("3.14 is positive whole: " + isPositiveWhole.test(3.14));
System.out.println("-2.0 is positive whole: " + isPositiveWhole.test(-2.0));
}
}
This example shows predicate combination with Combining Predicates with OR
The Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Define individual predicates
DoublePredicate isNegative = n -> n < 0;
DoublePredicate isGreaterThan100 = n -> n > 100;
// Combine with OR
DoublePredicate isNegativeOrLarge = isNegative.or(isGreaterThan100);
System.out.println("-5.0 matches: " + isNegativeOrLarge.test(-5.0));
System.out.println("150.0 matches: " + isNegativeOrLarge.test(150.0));
System.out.println("50.0 matches: " + isNegativeOrLarge.test(50.0));
}
}
This example demonstrates predicate combination with Negating a Predicate
The Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Original predicate
DoublePredicate isEven = n -> n % 2 == 0;
// Negated predicate
DoublePredicate isOdd = isEven.negate();
System.out.println("4.0 is odd: " + isOdd.test(4.0));
System.out.println("7.0 is odd: " + isOdd.test(7.0));
System.out.println("0.0 is odd: " + isOdd.test(0.0));
}
}
This example shows predicate negation with Using DoublePredicate with StreamsDoublePredicate is commonly used with DoubleStream for filtering operations. The filter method accepts a DoublePredicate to include elements in the stream. Main.java
package com.zetcode;
import java.util.Arrays;
import java.util.stream.DoubleStream;
public class Main {
public static void main(String[] args) {
double[] values = {1.5, -2.3, 4.7, 0.0, 10.2, -5.6, 7.8};
// Define predicate for filtering
DoublePredicate inRange = n -> n >= 0 && n <= 10;
// Filter stream using predicate
double[] filtered = DoubleStream.of(values)
.filter(inRange)
.toArray();
System.out.println("Original: " + Arrays.toString(values));
System.out.println("Filtered: " + Arrays.toString(filtered));
}
}
This example shows DoublePredicate usage with DoubleStream. We define a range check predicate and use it to filter values. Only numbers between 0 and 10 (inclusive) pass through the filter. Chaining Multiple PredicatesDoublePredicate methods can be chained to create complex conditions. This enables building sophisticated filters from simple predicates. Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Define base predicates
DoublePredicate isPositive = n -> n > 0;
DoublePredicate hasFraction = n -> n != Math.floor(n);
DoublePredicate notTooLarge = n -> n < 100;
// Chain predicates
DoublePredicate complexCondition = isPositive
.and(hasFraction)
.and(notTooLarge);
System.out.println("3.14 matches: " + complexCondition.test(3.14));
System.out.println("5.0 matches: " + complexCondition.test(5.0));
System.out.println("150.5 matches: " + complexCondition.test(150.5));
System.out.println("-2.7 matches: " + complexCondition.test(-2.7));
}
}
This example demonstrates predicate chaining. We combine three conditions: positive, has fractional part, and not too large. The complex condition only matches numbers that satisfy all three criteria. Practical Example: Data ValidationDoublePredicate is useful for data validation scenarios. We can create reusable validation rules for double values in business logic. Main.java
package com.zetcode;
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Define validation predicates
DoublePredicate isValidTemperature = t -> t >= -50 && t <= 50;
DoublePredicate isValidPercentage = p -> p >= 0 && p <= 100;
// Test values
double[] temps = {25.5, -60.2, 45.0, 55.1};
double[] percents = {75.3, -5.0, 100.0, 101.5};
System.out.println("Temperature validation:");
for (double t : temps) {
System.out.printf("%.1f°C: %s%n", t, isValidTemperature.test(t));
}
System.out.println("\nPercentage validation:");
for (double p : percents) {
System.out.printf("%.1f%%: %s%n", p, isValidPercentage.test(p));
}
}
}
This practical example shows DoublePredicate for data validation. We define rules for valid temperatures and percentages. The predicates are then used to validate arrays of values, demonstrating real-world applicability. SourceJava DoublePredicate Interface Documentation In this article, we've covered the essential methods and features of the Java DoublePredicate interface. Understanding these concepts is crucial for working with primitive double values in functional programming contexts. 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 01402503 — 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. |