2

I am writing the image processing program, and I have a problem with a list.

I have a list with Points.

     class Point1{
        private int x;
        private int y;

        Point1(int x,int y)
        {
            this.x = x;
            this.y = y;
        }

       int getX(){ return this.x; }
       int getY() {return this.y; }
    }

    ArrayList<Point1> list = new ArrayList();

And now, I create new Point, for example new Point(4,3); I want to check if there is a point in my list which has the same coordinates. The problem is that

    list.contains(Object a) 

is checking if if on my list there is a particular object. It will work if I put

    Point1 first = new Point(1,1);  
    list.add(first);
    list.contains(first) // and this is true

but:

    Point second = new Point(2,2);
    list.add(second);
    list.contains(new Point(2,2)); <- false

how can I check it?

user3131037
  • 395
  • 1
  • 4
  • 18

2 Answers2

3

You need to override equals on your Point class. Your Point class must be responsible for determining whether another Point object is "equal" to the current object. An ArrayList will call equals to determine if the object passed in to contains is "equal" to any item in the list.

If you don't override equals, then Point will inherit equals from Object, which will simply see if it's the same exact object. That's why your first code "works", because you are re-using first. It also explains why your second code doesn't "work", because you used a different object.

Also, if you override equals, it's best to override hashCode also (and vice versa).

rgettman
  • 167,281
  • 27
  • 248
  • 326
  • "it's best to override" - more precociously, two objects that are equals must return the same hash code. Thus, usually when overriding `equals`, `hashCode` must be overridden. – Steve Kuo May 29 '14 at 00:47
  • Eclipse will generate hashcode and equals implementations that are pretty decent. If that is not an option, apache commons-lang (3.x) has utilities for building hash codes and equals methods. – Brett Okken May 29 '14 at 01:40
1

List determine whether two objects are equal by using equals method.

If your class won't override public boolean equals(Object o){..} method, it will will inherit it from closest supertype which provides that implementation.

If your class doesn't extend any other class, it will means it implicitly extends Object class. So it will inherit equals implementation from it. Problem is that this implementation looks like:

public boolean equals(Object obj) {
    return (this == obj);
}

so it uses reference equality operator ==. This means that equals will only return true if object will be compared with itself (references to other objects will always be different than reference to this object).

In your case your equals method to return true also for other instances of Point1 if their state (value of x and y) is equal.

@Override
public boolean equals(Object other) {
    if(this == other)
        return true;
    if(!other instanceof Point1)
        return false;
    Point1 otherPoint= (Point1)other;
    if(this.x == otherPoint.getX() && this.y == otherPoint.getY())
        return true;
    return false;
}

And you can get the result you want: enter image description here

BTW while overriding equals method we should also override hashcode method. See Why do I need to override the equals and hashCode methods in Java?

Pshemo
  • 113,402
  • 22
  • 170
  • 242
William Lei
  • 48
  • 1
  • 7