Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to implement DoublePredicate using lambda and method reference in Java?
DoublePredicate is a built-in functional interface defined in java.util.function package. This interface can accept one double-valued parameter as input and produces a boolean value as output. DoublePredicate interface can be used as an assignment target for a lambda expression or method reference. This interface contains one abstract method: test() and three default methods: and(), or() and negate().
Syntax
<strong>@FunctionalInterface
public interface DoublePredicate {
boolean test(double value)
}</strong>
Example of lambda expression
import java.util.function.DoublePredicate;
public class DoublePredicateLambdaTest {
public static void main(String args[]) {
<strong>DoublePredicate</strong> doublePredicate = (double input) <strong>-> { // lambda expression</strong>
if(input == 2.0) {
return true;
} else
return false;
};
boolean result = doublePredicate.<strong>test</strong>(2.0);
System.out.println(result);
}
}
Output
<strong>true</strong>
Example of method reference
import java.util.function.DoublePredicate;
public class DoublePredicateMethodRefTest {
public static void main(String[] args) {
<strong>DoublePredicate </strong>doublePredicate = <strong>DoublePredicateMethodRefTest::test</strong>; <strong>// method reference</strong>
boolean result = doublePredicate.<strong>test</strong>(5.0);
System.out.println(result);
}
static boolean test(double input) {
if(input == 5.0) {
return true;
} else
return false;
}
}
Output
<strong>true</strong>
Advertisements
