私はクラス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() 関数が台無しになる可能性はありますか?
同様に、セット内に trueを返すcollection.contains(o)
オブジェクトがある場合でも、呼び出しは失敗します。X
o.equals(X)
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()
オーバーライドされません。問題?
更新
hashCode()
は次のとおりです。
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((sequence == null) ? 0 : sequence.hashCode());
return result;
}
しかし、それでも問題は解決しません。