java.util.function.DoubleToIntFunction is a Functional Interface. It has only one method, and that is an abstract method.
This abstract method is declared to accept one double value as an input-parameter, and it returns an int value. Both, input and output are primitives.
These classes are specialized versions for primitive. As JVM doesn’t need to do boxing and unboxing.
JVM has specific opcodes to handle primitive types. If you are working with primitive types, then use of these specialized classes is recommended. Because, that will give you an optimized code compared to wrapper classes.
This is the method declaration of abstract function in DoubleToIntFunction.
- int applyAsInt(double dblValue) :- takes an int and returns a double.
I don’t know in which conditions, it can be used. But, I am sure this interface is going to be useful in some cases.
like we can use it to downcast a double to an int.
int applyAsInt(double value) { return (int) value; }
In this article, we will be using this method to return hashcode, for a Double value. This is inefficient code, not-recommended by me. It converts a double primitive to a wrapper class of type Double and then invokes hashcode function of that class.
//Create a lambda using method reference DoubleToIntFunction getIntFromDouble = Double::hashCode;
We will be using three double literals to test the above lambda.
double dbl_1 = 1.1; double dbl_2 = 2.1; double dbl_3 = -13.1;
Two of the double values are positive and one is negative. Just to see, if positive and negative values make any difference.
This is our utility method,
private static void printHashCodes(DoubleToIntFunction hashcodeGenerator, double ... dblValues) { for (double val : dblValues) { out.format("Doouble value %+5.2f has haschode -> %+d \n", val, hashcodeGenerator.applyAsInt(val)); } }
which will loop through all the double literals and call our lambda over them.
I ran the test multiple times, to make sure the values of hashcodes are not the same. As, you can see, the hashcode value is different for the negative double values but it is the same for the positive values.
Test Run 1 :-
Doouble value 1.10 has haschode -> -1503133693 Doouble value 2.10 has haschode -> -1932787711 Doouble value 3.10 has haschode -> -1933311999
Test Run 2 :-
Doouble value +1.10 has haschode -> -1503133693 Doouble value +2.10 has haschode -> -1932787711 Doouble value +3.10 has haschode -> -1933311999
Test Run 3 :-
Doouble value +1.10 has haschode -> -1503133693 Doouble value +2.10 has haschode -> -1932787711 Doouble value -3.10 has haschode -> +214171649
Test Run 4 :-
Doouble value +1.10 has haschode -> -1503133693 Doouble value +2.10 has haschode -> -1932787711 Doouble value -13.10 has haschode -> -216465408
Complete Code :-
import java.util.function.DoubleToIntFunction; import static java.lang.System.out; public class DoubleToIntFunctionTest{ public static void main(String ... args) { //Create a lambda using method reference DoubleToIntFunction getIntFromDouble = Double::hashCode; double dbl_1 = 1.1; double dbl_2 = 2.1; double dbl_3 = -13.1; printHashCodes(getIntFromDouble, dbl_1, dbl_2, dbl_3); } private static void printHashCodes(DoubleToIntFunction hashcodeGenerator, double ... dblValues) { for (double val : dblValues) { out.format("Doouble value %+5.2f has haschode -> %+d \n", val, hashcodeGenerator.applyAsInt(val)); } } }