hashCode()用于HashMap中使用的对象数组 [英] hashCode() for an array of objects for use in HashMap

查看:147
本文介绍了hashCode()用于HashMap中使用的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个类,希望使用 Foo1 作为 HashMap 中的键。如果它们的 Foo2 对象相等,并且 Foo2 Foo1 如果字节数组满足 Arrays.equals(),则$ c>对象是相等的。



对于 Foo1,我不太确定为 hashCode()方法做些什么。我只需要总结它的每个 Foo2 对象的哈希码,或者这是低效的吗?

  public class Foo1 {

Foo2 [] foo2_array;

@Override
public boolean equals(Object Other){

for(int i = 0; i< foo2_array.length; i ++){

if(!foo2_array [i] .equals(other.foo2_array [i])
返回false;
}

返回true;
}

@Override
public int hashCode(){

//在这里有什么?
}
}

public class Foo2 {

byte [] values;

@Override
public boolean equals(Object other){

return Arrays.equals(values,other.values);
}

@Override
public int hashCode(){

return Arrays.hashCode(values );
}
}


解决方案

您的哈希码应该使用与等于相同的一组属性,因为它不会破坏合约。



只要在 Foo2 Arrays.hashcode >

Al所以你不必循环遍历每个元素,你可以使用 Arrays.equals



Foo2 equals can看起来像这样类似于Foo1.equals

  @Override 
public boolean equals(Object obj){
如果(this == obj)
返回true;
if(obj == null)
return false;
if(getClass()!= obj.getClass())
return false;
Foo1 other =(Foo1)obj;
if(!Arrays.equals(foo2_array,other.foo2_array))
return false;
返回true;
}



<>和散列码类似于Foo1散列码

  @Override 
public int hashCode(){
return Arrays.hashCode(foo2_array);
}

另外,当实现equals时,请检查null的相同引用和对象有效性。 / p>

I Have the following two classes and want to use Foo1 as keys in a HashMap. Two Foo1 objects are equal if their Foo2 objects are equal, and Foo2 objects are equal if their byte arrays satisfy Arrays.equals().

I am not quite sure what to do for the hashCode() method for Foo1. Do I just need to sum up the hashcodes from each of its Foo2 objects or is this inefficient?

public class Foo1 {

  Foo2[] foo2_array;

  @Override
  public boolean equals(Object Other) {

     for (int i = 0; i < foo2_array.length; i++) {

        if (!foo2_array[i].equals(other.foo2_array[i])
          return false;
     }

     return true;
   }

   @Override
   public int hashCode() {

      // what to here?
   }
}

public class Foo2 {

  byte[] values;

  @Override
  public boolean equals(Object other) {

      return Arrays.equals(values, other.values);
  }

  @Override
  public int hashCode() {

     return Arrays.hashCode(values);
  }
}

解决方案

Your hashcode should use the same set of properties as equals for it not to break the contract.

Just use the Arrays.hashcode as done in Foo2

Also you dont have to loop through each element in your equals you can just use Arrays.equals

Foo2 equals can look like this similar to Foo1.equals

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Foo1 other = (Foo1) obj;
        if (!Arrays.equals(foo2_array, other.foo2_array))
            return false;
        return true;
    }

and hashcode similar to Foo1 hashcode

    @Override
    public int hashCode() {
        return Arrays.hashCode(foo2_array);
    }

Also while implementing equals do check for same reference and object validity for null.

这篇关于hashCode()用于HashMap中使用的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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