Java DoublePredicate Interface with examples

 297 total views,  1 views today

Welcome to Java 8 Series. In this tutorial, you will learn about the Java DoublePredicate Interface with examples.

DoublePredicate is a Functional interface introduced as part of Java 8. It is available in java.util.function package.

What is the use of DoublePredicate?

This interface is mainly used for evaluating an input of type double and returns an output of type boolean. Consider a case where we need to write a lambda expression which accepts one input of double type and perform an operation on the input and return a boolean.

Example

public class StaticReference {
	public static void main(String[] args) {
		Operation ops = (double input) -> {
			if (input == 1.0) {
				return true;
			} else
				return false;
		};
		boolean result = ops.validate(1.0);
		System.out.println(result);
	}

	interface Operation {
		boolean validate(double input);
	}
}

When you look at the above example, we have a function interface with an abstract method which accepts an argument of type double return a boolean value. So a lambda expression needs a target type which needs to be a functional interface and in this example, it is Operation interface.

Instead of creating our own function interface for this scenario, Java has provided us a DoublePredicate interface. Let now see in-depth details on Java DoublePredicate Interface with examples.

DoublePredicate Interface

@FunctionalInterface
public interface DoublePredicate

DoublePredicate interface contains an operation that accepts an input argument of type double and returns a boolean value

It is a functional interface and the functional method istest(double)

Method description

boolean test(double value)
value - the input argument

Example of DoublePredicate interface using lambda

public class StaticReference {
	public static void main(String[] args) {
		DoublePredicate doublePredicate = (double input) -> {
			if (input == 1.0) {
				return true;
			} else
				return false;
		};
		boolean result = doublePredicate.test(1.0);
		System.out.println(result);
	}
}

Example of DoublePredicate interface using a Method reference

public class StaticReference {
	public static void main(String[] args) {
		DoublePredicate doublePredicate = StaticReference::test;
		boolean result = doublePredicate.test(1.0);
		System.out.println(result);
	}

	static boolean test(double input) {
		if (input == 1.0) {
			return true;
		} else
			return false;
	}
}

If you want to write a lambda expression with an input argument of type double and boolean result, please use Java DoublePredicate interface.

References

Java 8 DoublePredicate official documentation

Thanks for reading. Please a comment and give us a thumbs up.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.