Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash a double in Java

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

like image 311
daveb Avatar asked Mar 10 '12 22:03

daveb


People also ask

What is double hashing with example?

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.

What is hashCode () in Java?

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.

How do you hash a number in Java?

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 is double hashing needed?

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.


1 Answers

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?

Examples:

Double.valueOf(42.5).hashCode();        //better answer to everything

Long.valueOf(Double.doubleToLongBits(42.5)).hashCode();
like image 103
Tomasz Nurkiewicz Avatar answered Sep 29 '22 17:09

Tomasz Nurkiewicz