2012-05-02 30 views
1

我一直在使用ArrayList爲我的項目存儲板球隊球員並訂購它們。 我開始考慮使用TreeSet,因爲它具有刪除重複項的優點。 但是我遇到的問題是,例如,如果我創建以下兩個球員:刪除TreeSet中的重複項

P p1 = new P("Jack","Daniel",33(age),180(height),78(weight),41(games played),2300 
(runs scored),41(dismisses)) 
P p2 = new P("Jack","Daniel",37(age),185(height),79(weight),45(games played),2560 
(runs scored),45(dismisses)) 

注意兩個球員有相同的姓和名,但一切是不同的。當我嘗試將這兩個球員添加到TreeSet中時,由於名稱的相似性,它會認爲它們是重複的,並刪除第二個球員。很顯然,我不希望這種情況發生,我希望「套裝」只有在他擁有的所有東西與其他玩家相同時才能移除玩家,而不僅僅是名字和姓氏。

有沒有辦法實現這一目標?

另外我的TreeSet需要一個Player對象。

回答

11

最初,這個答案忽略了一個事實,即TreeSet根據compareTo()進行比較,而不是equals()。已經做出編輯來解決這個問題。

您需要爲您的Player對象正確定義equals(),hashCode()compareTo()。 (因爲它是一個TreeSet,而不是一個HashSet,實施hashCode()不是那麼重要 - 但它是很好的做法。)

equals和hashCode需要考慮到所有領域。 Eclipse可以爲你自動生成一個類似於此的文件(Source> Generate hashcode and equals)。

如果你已經有一個自然排序順序不使用所有的字段,那麼你可以提供一個自定義的比較你的TreeSet。然而,即使你真的只想按字段的一個子集進行排序,也沒有任何東西阻止你按照所有字段進行排序(不感興趣的字段僅僅扮演有趣部分的一部分是相同的)。這裏需要注意的重要一點是,TreeSet不是由equals()方法決定,而是由compareTo() == 0決定。

下面是一個例子equals()方法:

@Override 
public boolean equals(Object obj) 
{ 
    if (this == obj) { 
    return true; 
    } 
    if (obj == null) { 
    return false; 
    } 
    if (getClass() != obj.getClass()) { 
    return false; 
    } 

    Player that = (Player) obj; 
    return this.age == that.age && 
     this.height == that.height && 
     this.weight == that.weight && 
     this.games == that.games && 
     this.runs == that.runs && 
     this.dismisses == that.dismisses && 
     this.given.equals(that.given) && 
     this.family.equals(that.family); 
} 

而這裏的散列碼:

@Override 
public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + this.age; 
    result = prime * result + this.dismisses; 
    result = prime * result + this.family.hashCode()); 
    result = prime * result + this.games; 
    result = prime * result + this.given.hashCode()); 
    result = prime * result + this.height; 
    result = prime * result + this.runs; 
    result = prime * result + this.weight; 
    return result; 
} 

最後,這裏有一個的compareTo:

public int compareTo(Player that) 
{ 
    int result; 

    result = this.family.compareTo(that.family); 
    if (result != 0)        // is the family name different? 
    { 
    return result;        // yes ... use it to discriminate 
    } 

    result = this.given.compareTo(that.given); 
    if (result != 0)        // is the given name different? 
    { 
    return result;        // yes ... use it to discriminate 
    } 

    result = this.age - that.age;     // is the age different? 
    if (result != 0) 
    { 
    return result;        // yes ... use it to discriminate 
    } 

    ... (and so on) ... 
    ... with the final one ... 

    return this.dismisses - that.dismisses;  // only thing left to discriminate by 
} 
+0

有沒有可以幫助我的例子? –

+0

我有一個compareTo方法,按名稱和ID對球員進行排序 –

+0

在這種情況下,我需要使用compareTo方法中的其他領域,比如玩過的遊戲,運行得分等嗎? –

0

類學生實現可比{

String name; 

public Student(String name) { 
    this.name=name; 

} 

public String toString(){ 
    return name; 
} 

public int compareTo(Student gStudent) { 
    if(!this.name.equals(gStudent.getName())) 
     return 1; 
    return 0; 
} 

private String getName() { 
    return name; 
} 

}