0

I am a high school student so I apologize for the terms I may misuse.

So I am making a slide puzzle game and working on the AI part. So I have a constructor that construct board and assign its hashcode such as 123456780. In my A* algorithm, I compare if the board that I generate (to find the solution) is already in the hashset. So I use contains method for it right? but how does the contains method works to check if the two boards are identical?.

    public Board()
    {
            board = new int [3][3];
            setPieces (board);
            hashCode = generateHashCode ();
    }

This is one of my constructor. In my board object, I have 2D array and the hashcode. But I wonder again, if the built-in contains method in Hash Set compare two boards hashcode. Or I need to write one.

Also, when I assign the hash code to a board, I should do it in my constructor right?

Thanks you

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
FamilyMaze
  • 61
  • 5
  • Did you check the documentation? – SLaks Jun 08 '14 at 16:44
  • what do you mean by documentation? – FamilyMaze Jun 08 '14 at 16:45
  • The documentation for the HashSet class, which answers all of your questions. – SLaks Jun 08 '14 at 16:46
  • @SLaks: Can I ask you a really quick question please? Apparently, I can override the hashCode () which I did not know... Do I use that method in my constructor? I mean, do I assign hash code in the constructor? Thanks – FamilyMaze Jun 08 '14 at 17:02
  • @FamilyMaze - If your problem has been solved, don't forget to upvote any answers you found helpful and accept the answer that best answered your question by clicking the grey tick. If none of the answers so far were adequate, you can post your own if you found another solution. – Rudi Kershaw Jun 16 '14 at 11:05

2 Answers2

1

As you've discovered, you need to return a hashcode for your object in the overridden hashCode() method.

You can either compute a hashcode in that method, or compute it in the ctor and store it in a field, then return the field in the method override.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • but if the hashcode is computed in constructor, the object MUST be immutable!!! The same applies if the hashcode is computed inside `hashcode()` but is cached, so it's not computed several times – Svetlin Zarev Jun 08 '14 at 17:13
  • @SvetlinZarev: Yes; you cannot mutate equality-dependent fields. – SLaks Jun 08 '14 at 17:13
0

Any collection that uses the 'Hash' prefix has all objects contained within the collection stored in groups by their hashcode. So when you call a method like contains() the collection loops through each hashcode group and checks whether the hashcode of the object you parsed in matches the group. When a match is found, the objects within that hashcode group are checked using the equals() method of the stored object until an Object's equals() method returns true.

If you do not override the hashcode() method, then each Object subclass is given a more or less unique hashcode by the Object class implementation of the hashcode() method. This can cause some problems if you have already overridden the equals() method, because if two objects are considered equal the contains() method might still not find it if the hashcodes do not match.

The hashcode contract:

When implementing hashcode() the Java Object API has laid out a contract or a set of specifications on how you should implement the hashcode method. And they are;

  • If hashcode() is run on an object multiple times, it must always return the same number.
  • If two objects are considered equal by the equals() method then their hashcode() methods must return the same value.
  • If two objects have the same hashcode, they do not have to be considered equal by their equals() methods.

References:

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77