1

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);
  }
}
1
  • How is your Foo1 logic different from that of Foo2? In both cases you want the array fields of each object to be component-wise equal. Why not use Arrays.equals/Arrays.hashCode in both cases? (BTW, there's no way Foo2 will compile as is without some casting) Commented May 25, 2012 at 4:34

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You essentially need to have some method that makes it likely that different objects will have different hash codes.

So depending on your data, you don't necessarily need to sum up the hashes of all the items in the array. You just basically need something "good enough to narrow things down".

I would put it like this: is there anything about your data that makes you suspect you couldn't just take, say, the hash code of the middle value of the array? Or maybe the combined hash codes of the first, last and middle items, for example?

(Things that would make you suspect you couldn't do that: if, say, your data had some special feature making a certain narrow subset of values occur as the middle element in the array.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.