为什么GetHashCode不是像.NET中的HashCode这样的属性?
我正在寻找具有以下属性的Perl字符串校验和函数:
$string)$hash),0 <= $hash <= 2^32-1保持(0到4294967295,匹配4字节MySQL unsigned int的大小)伪代码:
sub checksum {
my $string = shift;
my $hash;
... checksum logic goes here ...
die unless ($hash >= 0);
die unless ($hash <= 4_294_967_295);
return $hash;
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,校验和函数应该快速运行,并且应该在目标空间(0.. 2^32-1)中稍微均匀地生成值以避免冲突.在这个应用程序中随机碰撞是完全非致命的,但显然我想在可能的范围内避免它们.
鉴于这些要求,解决这个问题的最佳方法是什么?
我有一个有float田地的课.例如:
public class MultipleFields {
final int count;
final float floatValue;
public MultipleFields(int count, float floatValue) {
this.count = count;
this.floatValue = floatValue;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要能够按值比较实例.现在我该如何正确实施 equals&hashCode?
通常的方式来实现equals,并hashCode是只考虑各个领域.例如,Eclipse将生成以下内容equals:
public boolean equals(Object obj) {
// irrelevant type checks removed
....
MultipleFields other = (MultipleFields) obj;
if (count != other.count)
return false;
if (Float.floatToIntBits(floatValue) != Float.floatToIntBits(other.floatValue))
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
(和类似的hashCode,基本上计算count* 31 + Float.floatToIntBits(floatValue)).
这个问题是我的FP值受到舍入错误的影响(它们可能来自用户输入,来自数据库等).所以我需要一个"宽容"的比较.
常见的解决方案是使用epsilon值进行比较(参见 …
我只是想知道,如果a的密钥HashMap是可变的,下面的测试程序会证明这一点会发生什么,而我无法理解当equals和hashCodemethods返回true和相同的值时,为什么会hashmap.containsKey返回false.
public class MutableKeyHashMap {
public static void main(String []a){
HashMap<Mutable, String> map = new HashMap<Mutable, String>();
Mutable m1 = new Mutable(5);
map.put(m1, "m1");
Mutable m2 = new Mutable(5);
System.out.println(map.containsKey(m2));
m2.setA(6);
m1.setA(6);
Mutable m3 = map.keySet().iterator().next();
System.out.println(map.containsKey(m2)+" "+m3.hashCode()+" "+m2.hashCode()+" "+m3.equals(m2));
}
}
class Mutable {
int a;
public Mutable(int a) {
this.a = a;
}
@Override
public boolean equals(Object obj) {
Mutable m = (Mutable) obj;
return m.a == this.a ? true …Run Code Online (Sandbox Code Playgroud) 我正在使用hibernate并需要覆盖equals和hashCode().我选择使用google-guava的equals和hashCode助手.
我想知道我在这里是否遗漏了什么.
我有id/image和filePath的 get/set方法.
@Entity
@Table(name = "IMAGE")
public class ImageEntity {
private Integer idImage;
private String filePath;
@Override
public int hashCode() {
return Objects.hashCode(getFilePath());
}
@Override
public boolean equals(final Object obj) {
if(obj == this) return true;
if(obj == null) return false;
if(obj instanceof ImageEntity){
final ImageEntity otherImage = (ImageEntity) obj;
return Objects.equal(getFilePath(), otherImage.getFilePath());
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
进入继承并在这里有一个样本
我的同事压倒了这个equals()方法.我的回答是,你是否也覆盖了这个hashCode()方法?他的回答是因为我们不会使用哈希映射或哈希集,如果我们覆盖它应该不重要hashCode().那是对的吗?
何时合理的时间来做第4行而不是第3行?或者它们是完全冗余的调用?
1 Object o1 = new Object();
2
3 int hcObj = o1.hashCode();
4 int hcSys = System.identityHashCode(o1);
Run Code Online (Sandbox Code Playgroud) 所以我对hashcode()和equals()方法有疑问
假设我只是写了一个非常基本的程序来覆盖这两个方法
import java.util.*;
class Employee
{
private String name;
private int empid;
public Employee(String name,int empid)
{
this.name=name;
this.empid=empid;
}
public int getEmpid()
{
return empid;
}
public String getName()
{
return name;
}
public boolean equals(Object obj)
{
System.out.println("equals has just been called...");
Employee e1=(Employee)obj;
return ((name.equals(e1.name)) && (empid==e1.empid));
}
public int hashCode()
{
System.out.println("hashcode called...");
return empid;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,假设我编写了另一个类来添加和迭代HashSet中的元素
class Five
{
public static void main(String args[])
{
HashSet hs1=new HashSet();
hs1.add(new Employee("Alex",25));
hs1.add(new Employee("Peter",25));
hs1.add(new …Run Code Online (Sandbox Code Playgroud) 据该评论后,hascode中null objects能throw NPE或值zero.这是特定于实现的.但相同的实现中,为什么
Objects.hashcode和hascode(instance)返回不同的值.对于前:
public class EqualsTesting {
public static void main(String[] args){
String p1 =null;
String p2 = null;
System.out.println(Objects.hashCode(p1));
System.out.println(p2.hashCode());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
Exception in thread "main" java.lang.NullPointerException
at BinaryTrees.EqualsTesting.main(EqualsTesting.java:14)
Run Code Online (Sandbox Code Playgroud)
如果是这样的话,这会不会影响到key look-up在HashMap那里null Key-value pairs是允许的.(它可能既hash要bucket 0或throw a NPE)
我非常喜欢Java 7+写作hashCode()方式:
@Override
public int hashCode() {
Objects.hash(field1, field2);
}
Run Code Online (Sandbox Code Playgroud)
但它无法正常使用数组.以下代码:
@Override
public int hashCode() {
Objects.hash(field1, field2, array1, array2);
}
Run Code Online (Sandbox Code Playgroud)
将不会工作,因为array1和array2常规hashCode()而不是Arrays.hashCode()被调用.
如何Objects.hash()以正确的方式使用数组?