方法一、将remove(object)更改成使用迭代器或者下标删除
列如:
Iterator<对象> iterator = userList.iterator(); 除去自己
while (iterator.hasNext()) {
User u = iterator.next();
if (getCurrentUser().getId().equals(u.getId())) {
iterator.remove();//使用迭代器的删除
}
}
方法二:重写equals、hashCode方法
比如:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AudioDeviceListEntry that = (AudioDeviceListEntry) o;
if (mId != that.mId) return false;
return mName != null ? mName.equals(that.mName) : that.mName == null;
}
@Override
public int hashCode() {
int result = mId;
result = 31 * result + (mName != null ? mName.hashCode() : 0);
return result;
}