-2

I have this class point

class Point
{
    private int x;
    private int y;
    Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public int getX()
    {
        return x;
    }
    public int getY()
    {
        return y;
    }
    public int hashCode() 
    {
        return this.toString().hashCode();
    }
    
    public String toString() 
    {
        return ("[" + x + ", " + y + "]"); 
    }

    public boolean equals(Point point) 
    {
        return (this.x == point.getX() && this.y == point.getY());
    }
}

and when I try to make HashSets with them the sets end up with duplicate entries.

I tried creating a .hashCode() method but it didn't appear to work

I also tried adding an equals method and it still didn't work

Edit:

changing the equals() method to

public boolean equals(Object o) 
    {
        return (this.toString().equals(o.toString()));
    }

fixed it

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    You need to implement `equals` too. – tgdavies Aug 07 '23 at 22:34
  • public boolean equals(Point point) { return (this.x == point.getX() && this.y == point.getY()); } just tried it and it still doesn't work. – Jacob Schweiger Aug 07 '23 at 22:40
  • 2
    You have not implemented equals correctly. The required signature is `public boolean equals(Object)`. See any good tutorial. And use the @Override annotation; this would have told you that you had not in fact overridden Object.equals. – Arfur Narf Aug 07 '23 at 22:46
  • Your hash code is kind of weird, though it'll work. A cheaper calculation might be `31 * x + y`. – Arfur Narf Aug 07 '23 at 22:48
  • Thank you changing it to take in an object worked. I changed the equals method to public boolean equals(Object o) { return (this.toString().equals(o.toString())); } and it worked – Jacob Schweiger Aug 07 '23 at 22:48
  • 2
    Why the fascination with converting everything to strings? It's easier to compare integers. – Arfur Narf Aug 07 '23 at 22:49
  • becuase I can't run .getX or .getY on the Object o since it's not garinteed to have those methods. but since all objects have a .toString method that was allowed. – Jacob Schweiger Aug 07 '23 at 22:57
  • 1
    You only care to compare if the other object is a Point. And so your equals method should 1) first check that `o` isn't null. If it is, return false. Then that `o` is a Point object. If not, return false. Then if it passes those tests, you *KNOW* that it has a `getX()` and a `getY()` method, and you should call them. Don't use an uncontrollable kludge like `toString()` as this makes for brittle code. – Hovercraft Full Of Eels Aug 08 '23 at 01:39
  • 1
    Please see [How to override equals method in Java](https://stackoverflow.com/q/8180430/) as well as [Java (Equals Method)](https://stackoverflow.com/q/34411932/). Note that `toString()` is used mainly for debugging and should never be used for key production code design, including equals and hashCode. Your code might fall apart if you want subclass instance to still be allowed to be equal but have differences in `toString()`, and so what you're currently doing is a *very bad idea* ™ – Hovercraft Full Of Eels Aug 08 '23 at 01:43

0 Answers0