您需要以这种方式覆盖类 Thing 中的equals方法和hashCode方法:
public class Thing {
private String name;
private Integer num;
public Thing(String a, Integer b) {
name = a;
num = b;
}
public void setName(String name) {
this.name = name;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if((obj == null) || (obj.getClass() != this.getClass())){
return false;
}
Thing that = (Thing)obj;
// Use the equality == operator to check if the argument is the reference to this object,
// if yes. return true. This saves time when actual comparison is costly.
return num == that.num &&
(name == that.name || (name != null && name.equals(that.name)));
}
/**
* This method returns the hash code value for the object on which this method is invoked.
* This method returns the hash code value as an integer and is supported for the benefit of
* hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must
* be overridden in every class that overrides the equals method.
*
* @return
*/
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + num;
hash = 31 * hash + (null == name ? 0 : name.hashCode());
return hash;
}
}
然后你可以这样使用它:
ArrayList<Thing> myList = new ArrayList<>();
Thing first = new Thing("Star Wars", 3);
if(!myList.contains(first)){
myList.add(first);
}
Thing second = new Thing("Star Wars", 1);
if(!myList.contains(second)){
myList.add(second);
}
就我而言,我使用 LinkedHashSet 来维护插入顺序,因为我认为这会更有效率。我没有用 ArrayList 尝试这个例子。
有关更多信息,您可以从此处阅读:为什么我以这种方式覆盖 equals 和 hashCode