[英]Java: Problems with TreeSet
我有一堂Odp。 我想使用TreeSet保留Odp對象的排序集合。 但是,我一直遇到問題。
public class OdpStorage {
private TreeSet<Odp> collection = new TreeSet<Odp>();
public addOdp(Odp o) {
return collection.add(o);
}
public int size() {
return collection.size();
}
}
如果collection.add(Odp o)已經在樹中,它應該什么也不做,對吧? 不知何故,此單元測試失敗:
OdpStorage ts = new OdpStorage();
Odp ftw = new Odp("LOL");
Odp ktr = new Odp("OMG");
ts.addOdp(ftw);
ts.addOdp(ftw); //should do nothing
ts.addOdp(ftw); //should do nothing
ts.addOdp(ftw); //should do nothing
ts.addOdp(ktr);
assertEquals(2, ts.size());
斷言失敗。 它期望為2,但返回值為5。為什么? odp.equals()函數可能會搞砸嗎?
同樣,即使集合X
有一個對象的o.equals(X)
返回true,調用collection.contains(o)
也會失敗。
Odp的.equals()函數:(由Eclipse生成)
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Odp))
return false;
Gene other = (Odp) obj;
if (sequence == null) {
if (other.sequence != null)
return false;
} else if (!sequence.equals(other.sequence))
return false;
return true;
}
相比於:
/**
* this = g0
* if they are equal, g1 is presumed to come first
*
* @return -1 if g0 comes before g1; 1 if g0 comes after g1
*/
@Override
public int compareTo(Odp g1) {
if (sequence.length() < g1.getSeq().length()) {
return -1;
}
else if (sequence.length() > g1.getSeq().length()) {
return 1;
}
if (sequence.compareTo(g1.getSeq()) < 0) {
return -1;
}
return 1;
}
hashCode()
不會被覆蓋。 問題?
UPDATE hashCode()
如下:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((sequence == null) ? 0 : sequence.hashCode());
return result;
}
但這仍然不能解決問題。
你的compareTo
實現永遠不會返回0。當對象實例相等時,它應該返回0。
看來您的collection.add(o)
在支持TreeMap
找不到該對象。 您的Odp
實現Comparable
還是在您已實現compare
方法的TreeSet
上設置默認的Comparable
? 如果是這樣,則需要確保您的compareTo
(對於Comparable
),或者如果傳入的對象equals
,則Comparator
compare
方法將返回0
。
編輯(以回應您對原始帖子的評論):
建議您每當重寫equals()
時都重寫HashCode()
equals()
EDIT2響應您的compareTo
實現:
如果g0
和g1
相等,則應返回0。這是問題的根源。
進行同伴清理,如果/其他太多。 用很多條件測試來代替它。 如果所有測試均通過,則返回true ...是的,它具有“ goto”語句,但非常易於閱讀,甚至在沒有太多嵌套的情況下也可以根據需要輕松插入新條件。 嵌套if / elses是邪惡的。 使用“ elses”是邪惡的,幾乎永遠不需要。
@Override
public boolean equals(final Object object) {
boolean equals = false;
do {
if (this == object) {
equals = true;
break;
}
if (false == super.equals(object)) {
break;
}
final DocumentView view = Unsafe.cast(object);
if (false == this.document.equals(view.document)) {
break;
}
if (this.revision != view.revision) {
break;
}
if (false == this.user.equals(view.user)) {
break;
}
if (false == this.timestamp.equals(view.timestamp)) {
break;
}
equals = true;
} while (false);
return equals;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.