-2

Below is some code that sees if there is a path in a maze from the top-left to the bottom right. My question with this code is where we check failedPoints.contains(p): how could we ever get true?

Right before we do if(failedPoints.contains(p)) we are creating a new Point object. Even if the new Point object has the same row and col values as another Point in failedPoints, the objects will be different, so shouldn't failedPoints.contains(p) always return false?

boolean getPath(boolean[][] maze, int row, int col, ArrayList<Point> path, HashSet<Point> failedPoints){
   /*if out of bounds or not available, return*/
   if(col<0 || row<0 || !maze[row][col]){
      return false;
   }
   Point p = new Point(row,col);
   /* If we've already visited this cell return*/
   if(failedPoints.contains(p)){
      return false;
   }

   boolean isAtOrigin = (row == 0) && (col == 0);

   /*If there's a path from start to my current location, add my location.*/
   if(isAtOrigin || getPath(maze,row,col -1, path, failedPoints) || getPath(maze,row-1, col, path,failedPoints)){
      path.add(p);
      return true;
   }
   failedPoints.add(p); //Cache result
   return false;
}
Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
Matt
  • 501
  • 4
  • 14
  • Have you read what `contains` does? What is unclear? – Tom Aug 03 '17 at 21:36
  • .contains will check if the object is contained within the hashset. But if I create a new object that has values the same as another object within the hashset, hashset.contains would still return false if the objects are different even if their properties may be the same – Matt Aug 03 '17 at 21:38
  • Thats what is confusing me – Matt Aug 03 '17 at 21:38
  • 2
    *".contains will check if the object is contained within the hashset."* - Why do you ignore important information? From the JavaDoc: **"if this set contains an element e such that (o==null ? e==null : o.equals(e))"**, so it is obviously important how `Point` implemented `equals` (and `hashcode`). Have you read it's source code? – Tom Aug 03 '17 at 21:40
  • 1
    See also: "[Why do I need to override the equals and hashCode methods in Java?](https://stackoverflow.com/questions/2265503)". – Kevin J. Chase Aug 03 '17 at 23:05

1 Answers1

2

It depends on the definition of the Point class.

Take a look at the HashSet.contains Javadocs:

public boolean contains(Object o)

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).

So if the Point class defines an equals method that checks to see if its row and column are the same as the row and column of the other point (and also defines a hashCode method that doesn't break the contract), then failedPoints.contains() will return true if another Point that equals the one being tested is present in the set.

Community
  • 1
  • 1
xyzzy
  • 70
  • 7
  • .equals will check if the two objects are identical not if they share the same property values – Matt Aug 03 '17 at 22:08
  • 1
    @Matt: The [`.equals` docs](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) disagree. It sounds like you are thinking of `==`, which is not used in the general case specifically _because_ it compares object identities instead of object values. – Kevin J. Chase Aug 03 '17 at 23:03