使用哈希码获取唯一ID [英] Using hashcode for a unique ID

查看:244
本文介绍了使用哈希码获取唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Java的系统中工作,在该系统中,我需要为视觉显示中的某些元素设置ID.一类元素是字符串,因此我决定使用String.hashCode()方法来获取这些元素的唯一标识符.

I am working in a java-based system where I need to set an id for certain elements in the visual display. One category of elements is Strings, so I decided to use the String.hashCode() method to get a unique identifier for these elements.

但是,我遇到的问题是,如果id为负,并且String.hashCode经常返回负值,那么我正在borks中工作的系统.一种快速的解决方案是仅在哈希码调用周围使用Math.abs()来确保得到肯定的结果.我想知道这种方法是,两个不同的元素具有相同的哈希码的机会是什么?

The problem I ran into, however, is that the system I am working in borks if the id is negative and String.hashCode often returns negative values. One quick solution is to just use Math.abs() around the hashcode call to guarantee a positive result. What I was wondering about this approach is what are the chances of two distinct elements having the same hashcode?

例如,如果一个字符串返回的哈希码为-10,而另一个字符串返回的哈希码为10,则将发生错误.在我的系统中,我们所谈论的对象集合通常不超过30个元素,因此我不认为这确实是一个问题,但我对数学的含义感到好奇.

For example, if one string returns a hashcode of -10 and another string returns a hashcode of 10 an error would occur. In my system we're talking about collections of objects that aren't more than 30 elements large typically so I don't think this would really be an issue, but I am curious as to what the math says.

推荐答案

哈希码可以看作是伪随机数.从统计学上讲,使用正int哈希码时,当总体大小约为54K( any int为77K)时,任意两个元素之间发生冲突的机会达到50%.有关各种哈希码大小的冲突概率,请参见生日问题概率表.

Hash codes can be thought of as pseudo-random numbers. Statistically, with a positive int hash code the chance of a collision between any two elements reaches 50% when the population size is about 54K (and 77K for any int). See Birthday Problem Probability Table for collision probabilities of various hash code sizes.

此外,您单独使用Math.abs()的想法存在缺陷:它并不总是返回正数!在2的补码算法中,Integer.MIN_VALUE的绝对值本身就是!众所周知,"polygenelubricants"的哈希码就是这个值.

Also, your idea to use Math.abs() alone is flawed: It does not always return a positive number! In 2's compliment arithmetic, the absolute value of Integer.MIN_VALUE is itself! Famously, the hash code of "polygenelubricants" is this value.

这篇关于使用哈希码获取唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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