我同意它在身份证上。但是我在获取应该更新数据库的数据时遇到了麻烦。在这个带有用户的例子中,equals 只查看了我创建的 ID。
interface DataEquals<T extends DataEquals> {
public boolean isDataEquals(T other)
}
User implements DataEquals<User> {
public boolean isDataEquals(User other) {
boolean b1 = getName().equals(other.getName());
boolean b2 = getAge().equals(other.getAge());
boolean b3 = getAccount().equals(other.getAccount());
return b1 && b2 && b3;
}
}
有了这个,我们可以拥有这个。
public class ListChanges<T extends DataEquals<T>> {
private List<T> added = new ArrayList<T>();
private List<T> removed = new ArrayList<T>();
private List<T> changed = new ArrayList<T>();
private List<T> unchanged = new ArrayList<T>();
public ListChanges() {
super();
}
public List<T> getAdded() {
return added;
}
public List<T> getChanged() {
return changed;
}
public List<T> getRemoved() {
return removed;
}
public List<T> getUnchanged() {
return unchanged;
}
public boolean hasAnyChanges() {
return added.size()>0 || removed.size()>0 || changed.size()>0;
}
public void parse(List<T> oldList,List<T> newList) {
for (T oldObj : oldList) {
int index =newList.indexOf(oldObj);
if (index==-1) {
removed.add(oldObj);
} else {
T newObj = newList.get(index);
if (newObj.isDataEquals(oldObj)) {
unchanged.add(oldObj);
} else {
changed.add(newObj);
}
}
}
for (T newObj : newList) {
if (oldList.indexOf(newObj)==-1) {
added.add(newObj);
}
}
}
}
然后我们可以这样做
List<User> oldList = ....;
List<User> newList = ...;
ListChanges<User> listChanges = new ListChanges<User>();
listChanges.parseChanges(oldList,newList);
你同意这是一种方法吗????