I was wondering how to hash a double in Java? I have hashed other primitive data and objects. I thought I could use the hashcode method? From what I have seen this looks quite complex. I came across something about creating a seed.
I was wondering any ideas on how to go about this. Hoping to put in with the rest of my hashcode for the class that has the double?
I was wondering if there are issues with me trying to hash arraylists, arrays and other objects in java. Some of my classes contain arraylists.
Many Thanks
Double Hash Function The first hash function determines the initial location to located the key and the second hash function is to determine the size of the jumps in the probe sequence. The following function is an example of double hashing: h(key, i) = (firstHashfunction(key) + i * secondHashFunction(key)) % tableSize.
hashCode in Java is a function that returns the hashcode value of an object on calling. It returns an integer or a 4 bytes value which is generated by the hashing algorithm. The process of assigning a unique value to an object or attribute using an algorithm, which enables quicker access, is known as hashing.
The HashCode for an Integer can be obtained using the hashCode() method in Java. This method does not accept any parameters and it returns a hash code value of the Integer object.
Why use double hashing? Double hashing is useful if an application requires a smaller hash table since it effectively finds a free slot. Although the computational cost may be high, double hashing can find the next free slot faster than the linear probing approach.
Double.hashCode()
complex? It basically converts double
into a long
(no magic here, after all they both are simply 64-bit values in memory) and computing long
hash is quite simple. The double
-> long
conversion is done via public static doubleToLongBits()
. What is complex about this?
Double.valueOf(42.5).hashCode(); //better answer to everything
Long.valueOf(Double.doubleToLongBits(42.5)).hashCode();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With