Remove duplicates in the ArrayList of custom objects
I’m trying to remove duplicate objects from an array.
I have my habit, and it consists of two doubles: x and y.
What I want to do is remove duplicates ((x &&y) == (x1 &
& y1)) if x == x1 I want to keep objects with higher y.
ArrayList<MyObject> list = [x(0),y(0)], [x(0),y(0)], [x(0.5),y(0.5], [x(0.5),y(0.6)], [x(1),y(1)];
ArrayList<MyObject> results = [x(0),y(0)], [x(0.5),y(0.6)], [x(1),y(1)];
I
tried implementing the equals method, but I don’t know how to use it :
public boolean equals(Object obj) {
if (obj == null || !( obj instanceof MyObject)) {
return false;
}
return (this.x == ((MyObject)obj).x);
}
Lists are always sorted using Collections.sort by x.
Thank you.
Solution
Given MyObject
:: like this
class MyObject {
private final double x;
private final double y;
public MyObject(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyObject myObject = (MyObject) o;
if (Double.compare(myObject.x, x) != 0) return false;
if (Double.compare(myObject.y, y) != 0) return false;
return true;
}
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(x);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}
You can implement a unique method that returns a list of only unique
elements:
private List<MyObject> unique(List<MyObject> list) {
List<MyObject> uniqueList = new ArrayList<>();
Set<MyObject> uniqueSet = new HashSet<>();
for (MyObject obj : list) {
if (uniqueSet.add(obj)) {
uniqueList.add(obj);
}
}
return uniqueList;
}
and unit test it to verify that it works:
@Test
public void removeDups() {
List<MyObject> list = Arrays.asList(new MyObject(0, 0), new MyObject(0, 0), new MyObject(0.5, 0.5), new MyObject(0.5, 0.6), new MyObject(1, 1));
List<MyObject> results = Arrays.asList(new MyObject(0, 0), new MyObject(0.5, 0.5), new MyObject(0.5, 0.6), new MyObject(1, 1));
assertEquals(results, unique(list));
}
Note: It is important to implement equals
and hashCode
Because HashMap is used. But you should always do this in your custom class: provide the appropriate equals
and hashCode
implementation. By the way, I didn’t write those equals
and hashCode
methods. I have my IDE (IntelliJ) automatically generate them from the fields x and
y
of the class.