我有一个对象列表:
我已经编写了hashCode()和equals()方法:
但它没有应用它,而是使用了object.equals()方法而非我的方法。
List<City> cities = city.getList();
我希望能够去除重复项(所谓重复项是指具有相同参数name
值但不同的id
和其他参数的对象)。
我已经编写了相关代码:
for(City c: cities) {
System.out.println("analise " + c.name);
if(!temp.contains(c)) {
temp.add(c);
}
}
我已经编写了hashCode()和equals()方法:
@Override
public int hashCode() {
return id.hashCode();
}
...
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof GenericDictionary))return false;
GenericDictionary otherMyClass = (GenericDictionary) other;
if(this.name == otherMyClass.name) {
return true;
} else {
return false;
}
}
但它没有应用它,而是使用了object.equals()方法而非我的方法。