DoubleUnaryOperator Interface in Java
Last Updated :
18 Nov, 2019
The
DoubleUnaryOperator Interface is a part of the
java.util.function package which has been introduced since Java 8, to implement
functional programming in Java. It represents a function which takes in one argument and operates on it. Both its argument and return type are of double data type.It is very similar to using an object of type
UnaryOperator<Double>.
The lambda expression assigned to an object of DoubleUnaryOperator type is used to define its
applyAsDouble() which eventually applies the given operation on its argument.
Functions in DoubleUnaryOperator Interface
The DoubleUnaryOperator interface consists of the following functions:
1. identity()
This method returns a DoubleUnaryOperator which takes in one double value and returns it. The returned DoubleUnaryOperator does not perform any operation on its only value.
Syntax:
static DoubleUnaryOperator identity()
Parameters: This method does not take in any parameter
Returns: A DoubleUnaryOperator which takes in one value and returns it.
Below is the code to illustrate identity() method:
Program
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
DoubleUnaryOperator
op
= DoubleUnaryOperator.identity();
System.out.println(op.applyAsDouble(12.0));
}
}
2. applyAsDouble()
This method takes in one double value, performs the given operation and returns a double-valued result.
Syntax:
double applyAsDouble(double operand)
Parameters: This method takes in one double valued parameter
Returns:: It returns a double valued result.
Below is the code to illustrate applyAsDouble() method:
Program
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
DoubleUnaryOperator op = a -> 2 * a;
System.out.println(op.applyAsDouble(12.0));
}
}
3. addThen()
It returns a composed DoubleUnaryOperator wherein the parameterized operator will be executed after the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
Syntax:
default DoubleUnaryOperator andThen(DoubleUnaryOperator after)
Parameters: This method accepts a parameter
after which is the operation to be applied after the current one.
Return Value: This method returns a
composed DoubleUnaryOperator that applies the current operation first and then the after operation.
Exception: This method throws
NullPointerException if the after operation is null.
Below is the code to illustrate addThen() method:
Program 1:
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
DoubleUnaryOperator op = a -> 2 * a;
op = op.andThen(a -> 3 * a);
System.out.println(op.applyAsDouble(12.0));
}
}
Program 2: To demonstrate when NullPointerException is returned.
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
try {
DoubleUnaryOperator op = a -> 2 * a;
op = op.andThen(null);
System.out.println(op.applyAsDouble(12.0));
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Exception: java.lang.NullPointerException
4. compose()
It returns a composed DoubleUnaryOperator wherein the parameterized operation will be executed first and then the first one. If either operation throws an error, it is relayed to the caller of the composed operation.
Syntax:
default DoubleUnaryOperator compose(DoubleUnaryOperator before)
Parameters: This method accepts a parameter
before which is the operation to be applied first and then the current one
Return Value: This method returns a composed DoubleUnaryOperator that applies the current operator after the parameterized operator
Exception: This method throws
NullPointerException if the before operation is null.
Below is the code to illustrate compose() method:
Program 1:
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
DoubleUnaryOperator op = a -> a / 3;
op = op.compose(a -> a * 6);
System.out.println(op.applyAsDouble(12.0));
}
}
Program 2: To demonstrate when NullPointerException is returned.
Java
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String args[])
{
try {
DoubleUnaryOperator op = a -> a / 3;
op = op.compose(null);
System.out.println(op.applyAsDouble(12.0));
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Exception: java.lang.NullPointerException
Similar Reads
BinaryOperator Interface in Java
The BinaryOperator Interface<T> is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a binary operator which takes two operands and operates on them to produce a result. However, what distinguishes it fro
2 min read
Enumeration Interface In Java
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator.
3 min read
Java.util.function.DoubleBinaryOperator interface with Examples
The DoubleBinaryOperator interface was introduced in Java 8. It represents an operation on two double values and returns the result as a double value. It is a functional interface and thus can be used as a lambda expression or in a method reference. It is mostly used when the operation needs to be e
2 min read
Collection Interface in Java
The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
ConcurrentMap Interface in Java
The ConcurrentMap Interface is part of the Java Collections Framework and was introduced in JDK 1.5. It is designed for thread-safe concurrent access to its entries without compromising the consistency of the map. The interface resides in the java.util.concurrent package and extends the Map interfac
9 min read
DoubleFunction Interface in Java with Examples
The DoubleFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and produces a result of type R. This functional interface takes in only one
1 min read
Function Interface in Java
The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and produces a result. Hence, this functional interface takes in 2 generics, namely as follows:T:
6 min read
Evolution of Interface in Java
In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java
3 min read
Cloneable Interface in Java
The Java.lang.Cloneable interface is a marker interface. It was introduced in JDK 1.0. There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object.clone() method valid thereby making field-for-field copy. This interface allows the implementing class to
4 min read
BlockingQueue Interface in Java
The BlockingQueue Interface in Java is a part of the java.util.concurrent package and was introduced in Java 1.5. It is a specialized Queue implementation designed to handle thread-safe operations in concurrent environments. BlockingQueue is commonly used in Producer-Consumer patterns and other mult
11 min read