DoubleFunction Interface in Java with ExamplesThe java.util.function package, which was introduced with Java 8, includes the DoubleFunction Interface, which is used to support functional programming in Java. It stands for a function that generates a result of type R after receiving a double-valued input. There is only one generic accepted by this functional interface, which is: R: indicates the kind of this function's output. An object of the DoubleFunction type's lambda expression is used to create its apply() function, which ultimately applies the specified operation on its single input. Using an object of type Function<Double, R> is similar to this. The single function of the DoubleFunction interface is apply(). This method returns a result of type R and takes as input a double-valued argument. Syntax: Parameters: The individual parameter value that this method accepts is a double-valued argument. Returns: A value of type R is the result of this method. Example 1:A DoubleFunction that accepts a double input and returns its square is defined in this program using a lambda expression. This squaring reasoning is contained in the lambda phrase (double value) -> value * value. Next, this function is applied to a given value; in this example, the input value is 4.0, using the DoubleFunction's apply method. After squaring 4.0, the result is printed and stored in the variable res. Implementation: FileName: SquareFunctionExample1.java Output: The Square of the given value is 16.0 Example 2:The program that follows demonstrates how to transform a double value into a String representation using the DoubleFunction interface. A DoubleFunction that converts a double input into a string with a message like "The Double value is given by:" is known as a lambda expression. The exact double number 5.24 is subsequently converted into its string form using this function's apply method. The string "The Double value is given by: 5.24" is printed as the result. Implementation: FileName: DoubleToStringConversionExample2.java Output: The Double value is given by : 5.24 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India