我如何生成一个字符串的长散列? [英] How can i generate a long hash of a String?

查看:129
本文介绍了我如何生成一个字符串的长散列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,我希望为字符串生成 long ids(为了将这些字符串存储在 neo4j )。为了避免数据重复,我想为存储在 long 整数中的每个字符串生成一个id,对每个字符串应该是唯一的。我该怎么做? long 有64位。长度为9的 String 有72位。从鸽子洞原理 - 你不能得到一个独特的哈希长度为9个字符串到<$ c如果你仍然想要一个 long 哈希值:你可以通过<$ c $>只需要为 String-> int hash1()和<$ c两个标准[不同! $ c> hash2()并计算: hash(s)= 2 ^ 32 * hash1(s)+ hash2(s)


I have a java applciation in which I want to generate long ids for strings (in order to store those strings in neo4j). In order to avoid data duplication, I would like to generate an id for each string stored in a long integer, which should be unique for each string. How can I do that ?

解决方案

long has 64 bits. A String of length 9 has 72 bits. from pigeon hole principle - you cannot get a unique hashing for 9 chars long strings to a long.

If you still want a long hash: 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)

这篇关于我如何生成一个字符串的长散列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆