How can I generate a long hash of a string?

I have a Java application in which I want to generate longidentifiers for strings (to save these strings in neo4j ). To avoid data duplication, I would like to create an identifier for each row stored in longinteger, which should be unique for each row. How can i do this?

+5
source share
4 answers

longhas 64 bits. A Stringlength of 9 has 72 bits. from the principle of holes in the hand - you cannot get a unique hash for 9 lines of a long line before long.

If you still want a hash long: you can just take two standard [different!] Hash functions for String->int, hash1()and hash2()and calculate:hash(s) = 2^32* hash1(s) + hash2(s)

+4
source

Why don't you take a look at the function hashcode()for String and just accept it instead of using long values?

Btw. if there was a way to create a unique identifier for each line, then you would find a compression algorithm that could pack each line in 8 bytes (this is not possible by definition).

+6
source

:

String s = "some string";
long hash = UUID.nameUUIDFromBytes(s.getBytes()).getMostSignificantBits();
+5

, :

Or, as suggested earlier, check out the sources.

PS. Another way is to maintain a dictionary of strings: since you are unlikely to get 2 64 strings soon , you can have a perfect mapping. Please note that this mapping can also become a major bottleneck.

+1
source

All Articles