- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在知道有内置实用程序,例如 Apache commons lang 中的 HashCodeBuilder,但我试图了解如何自己实现它,并在 http://en.wikipedia.org/wiki/Java_hashCode() 遇到了 Employee 类的 hascode 函数的示例。
谷歌上的任何地方都建议使用相同的技术,例如将非零值与奇素数相乘,然后将其相加与实例变量(为实例变量执行此操作)。
问题:-
1)为什么我们不能将employeeId作为hascode返回,因为它永远是唯一的。它很简单并且服务于 hascode 目的。同意,如果它不是唯一的,我们可能需要这种技术。是这样吗?
2)即使员工 ID 不唯一,为什么建议乘以奇数素数?为什么取任何该死的整数都不是算不错吗?
更新:-
Peter 我运行了你提到的打印示例
[0、32、64、96、128、160、192、224、288、256、352、320、384]
[0、32、64、96、128、160、192、224、288、256、352、320、384]
i assume that output for now as yoy expected to understand the concept as you mentioned in your answer
[373、343、305、275、239、205、171、137、102、68、34、0]
[0、34、68、102、137、171、205、239、275、305、343、373]
正如您在评论中所建议的,该示例演示了即使是唯一的哈希码也可以最终出现在同一个存储桶中。这怎么办例子证明了这种行为?您的意思是整数 373 和整数 0 2 最终在同一个存储桶中吗?
素数在这个例子中有何帮助,而 34 又有何帮助?
最佳答案
why we can't return employeeId as hascode becoz it will aways be unique. Its simple and serves the hascode purpose. Agreed if it is not unique probably we need that kind of technique. Is that right?
它的独特性并不重要。乘以素数是将多个字段合并为一个 hashCode 的好方法,但听起来好像只有一个,所以不会有太大区别。
Even If employee id is not unique, why its suggested to multiply with odd prime number? why taking any damn integer is not considered good?
如果乘以偶数,hashCode 的最低位是多少?它有多随机/有用?
<小时/>注意:Integer 的每个 hashCode() 都是唯一的,但是获得整数值的正确组合,当它们减少到 HashMap 的容量时,它们实际上映射到同一个存储桶。在此示例中,条目以与添加顺序相反的顺序显示,因为每个条目都映射到同一个存储桶。
HashSet<Integer> integers = new HashSet<>();
for (int i = 0; i <= 400; i++)
if ((hash(i) & 0x1f) == 0)
integers.add(i);
HashSet<Integer> integers2 = new HashSet<>();
for (int i = 400; i >= 0; i--)
if ((hash(i) & 0x1f) == 0)
integers2.add(i);
System.out.println(integers);
System.out.println(integers2);
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
打印
[373, 343, 305, 275, 239, 205, 171, 137, 102, 68, 34, 0]
[0, 34, 68, 102, 137, 171, 205, 239, 275, 305, 343, 373]
关于java - 在java中实现好的hashCode函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209464/
class UserScoring implements Comparable { User user; int score; UserScoring(
当重写 Java 中的 equals() 和 hashcode() 方法时,为什么不经常使用它: public int hashCode() { return (int) this.hashC
给定java Object#hashCode文档快照: As much as is reasonably practical, the hashCode method defined by class
下面的代码(sign.hashCode())是给我签名的hashCode还是内存中对象的hash? try { PackageInfo packageInfo = getPackageMana
考虑: String[] segments = {"asdf", "qwerty", "blahblah", "alongerstring", "w349fe3434"}; String fullSt
在审查大型代码库时,我经常遇到这样的情况: @Override public int hashCode() { return someFieldValue.hashCode(); } 程序员不
在以下情况下,与下面的函数发生 HashCode 冲突的可能性有多大。 key[0]、key[1]、key[2]、key[3] 的随机整数值 使用具有以下约束的随机键值 键[0] <1,000,000
从 Java 7 开始,我们有了 o.hashCode(); Objects.hashCode(o); Objects.hash(o); 前两个与空检查大致相同,但最后一个是什么? When a si
这个问题已经有答案了: Objects.hash() vs Objects.hashCode(), clarification needed (3 个回答) 已关闭 6 年前。 一个简单、简短的问题:
我是否需要使用super.hashcode()来计算this.hashcode()? IDE(例如 IntelliJ Idea)可以生成 equals 和 hashcode。它可以使用 java.ut
class A { } class B extends A { void m1(){ System.out.println(this.hashCode());
我查看了Arrays.hashCode(char[] c)的源代码 我不太确定它适用的算法是否在所有情况下都能正常工作。 public static int hashCode(int a[])
我有两个表具有一对一的关系,如下所示: @Entity @Data @NoArgsConstructor @AllArgsConstructor public class Book { @Id
为什么stringObject的hashcode是我提供的字符串? String s = new String(); // here the hascode is 0. 但是当我获得我创建的某个对象的
public abstract class HolidayPackageVariant { private HolidayPackage holidayPackage; private String
这两个代码片段有什么区别? 片段 1: Object o = new Object(); int i = Objects.hashCode(o); 片段 2: Object o = new Objec
在 Java 8 中有一个类 java.util.Objects,其中包含 hashCode() 方法。同时 Google Guava 19 包含 com.google.common.base.Obj
我的一个类(class)中有以下方法。它只是 HashMap 的公共(public)包装器(名为 teamOfPlayer,具有 Player 对象的键和 Integer 对象的值),仅此而已。 pu
我在这里做错了什么? @Override public int hashCode() { HashCodeBuilder has
我有以下程序。 Employee employee1 = new Employee("Raghav1", 101); Employee employee2 = new Employee("Raghav
我是一名优秀的程序员,十分优秀!