[英]How to convert "Set functions" such as removeAll and addAll from Java to C++? Can it be done using erase or insert?
[英]removeAll from Interface Set
我想比較數據庫轉儲到xml和* .sql。 在解toRemove
, toRemove
和toAdd
僅在尺寸上有所不同。 toRemove
大小為3, toAdd
大小為4。但是在運行代碼后, removeAll
, toRemove
大小為3和toAdd
大小為4。這是怎么回事?
final DBHashSet fromdb = new DBHashSet(strURL, strUser, strPassword);
final DBHashSet fromxml = new DBHashSet(namefile);
Set<DBRecord> toRemove = new HashSet<DBRecord>(fromdb);
toRemove.removeAll(fromxml);
Set<DBRecord> toAdd = new HashSet<DBRecord>(fromxml);
toAdd.removeAll(fromdb);
更新:
public class DBRecord {
public String depcode;
public String depjob;
public String description;
public DBRecord(String newdepcode, String newdepjobe, String newdesc) {
this.depcode = newdepcode;
this.depjob = newdepjobe;
this.description = newdesc;
}
public String getKey() {
return depcode + depjob;
}
public boolean IsEqualsKey(DBRecord rec) {
return (this.getKey().equals(rec.getKey()));
}
public boolean equals(Object o) {
if (o == this)
return true;
if (o == null)
return false;
if (!(getClass() == o.getClass()))
return false;
else {
DBRecord rec = (DBRecord) o;
if ((rec.depcode.equals(this.depcode)) && (rec.depjob.equals(this.depjob)))
return true;
else
return false;
}
}
}
為了正確使用HashSet
(和HashMap
),必須按照以下約定實現hashCode()
:
您為DBRecord
提供的代碼不會覆蓋它,因此是問題所在。 您可能希望通過以下方式或類似方式覆蓋它:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + depcode.hashCode();
result = prime * result + depjob.hashCode());
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.