2012-02-27 72 views
1

我想創建根據值排序的唯一鍵值對的前5列表。LinkedHashSet中的唯一Java對象

我試圖創建一個HashMap但因爲我是從JSON讀取原始列表進行排序的Hashmap覆蓋的最後一個值,使他們關鍵還得最小的值,而不是最大的。

的解決方案是使用LinkedHashSet,以確保其唯一性,並保持秩序。但是因爲我存儲了一個鍵值對,所以我決定創建一個新類並將它們保存爲對象。

我知道,我必須實現媲美,但顯然沒有比較發生和LinkedHashSet是不是唯一的。

我的代碼是:

public class cellType implements Comparable<Object> { 

private String type; 
private double confidence; 

@Override 
public String toString() { 
    return "type=" + type + " - confidence=" + confidence ; 
} 

public cellType(String type, double confidence) { 
    super(); 
    this.type = type; 
    this.confidence = confidence; 
} 
public String getType() { 
    return type; 
} 
public void setType(String type) { 
    this.type = type; 
} 
public double getConfidence() { 
    return confidence; 
} 
public void setConfidence(double confidence) { 
    this.confidence = confidence; 
} 
@Override 
public boolean equals(Object obj) { 
    if (!(obj instanceof cellType)) { 
      return false; 
     } 
    cellType ct = (cellType) obj; 
    return type.equals(ct.getType()); 
} 
@Override 
public int compareTo(Object o) { 
    cellType ct = (cellType) o; 
    return type.compareTo(ct.getType()); 
} 

}

public static void main(String args[]) throws IOException, JSONException { 
    String freebaseAddress = "https://www.googleapis.com/freebase/v1/search?query="; 
    System.setProperty("https.proxyHost", "proxy"); 
    System.setProperty("https.proxyPort", "8080"); 
    JSONObject json = readJsonFromUrl(freebaseAddress + "apple"); 
    LinkedHashSet<cellType> rich_types = new LinkedHashSet<cellType>(); 
    JSONArray array = json.getJSONArray("result"); 
    for (int i = 0; i < array.length(); i++) { 
     if (array.getJSONObject(i).has("notable")) { 
      JSONObject notable = new JSONObject(array.getJSONObject(i) 
        .getString("notable")); 
      if (rich_types.size() <= 5) 
       rich_types.add(new cellType(notable.getString("name"), (Double) array.getJSONObject(i).get("score"))); 
     } 
    } 
    System.out.println(rich_types); 
} 

的輸出是:

[類型=君主 - 信心= 79.447838,類型=君主 - 信心= 58.911613, type = Monarch - confidence = 56.614368,type = Founding Figure - confidence = 48.796387,type = Politician - confidence = 38.921349,type = Queen consort - confidence = 36.1428 64]

+0

我已經通過使用HashMap和檢查項不存在將解決這個問題,但我不喜歡這樣的解決方案,我想這應該可以解決更優雅 – AhmadAssaf 2012-02-27 14:32:42

回答

1

我想你的意思是你要使用TreeMap中(MAP未設置)使用可比鍵對它們進行排序。 LinkedHashSet是保持添加順序的元素的集合。

這聽起來像你想要的是

if (rich_types.size() <= 5) { 
    cellType ct = new cellType(notable.getString("name"), (Double) array.getJSONObject(i).get("score")); 
    if(!rich_type.contains(ct)) 
     rich_types.add(ct); 
} 
+0

與TreeMap我得到了與HashMap相同的行爲,分配給密鑰的值被覆蓋,例如上面的Monarch = 56.614 ..而不是第一個最高值是7944 .. – AhmadAssaf 2012-02-27 14:28:52

+0

如果您的「類型」是同樣,他們將被視爲重複。如果它們不重複,則需要給它們不同的密鑰。如果您需要允許重複,則可以使用List來代替。 – 2012-02-27 14:37:28

+0

對不起,我在我以前的評論中不清楚,他們被認爲是重複的,並且該列表現在是唯一的,但是該鍵的值被發現的最後一個重複值覆蓋。並且我不希望有那個 – AhmadAssaf 2012-02-27 14:42:35

1

你需要實現的hashCode()了。
任何人甚至認爲實現equals()和hashCode()需要至少讀取Effective Java或更高版本的this chapter整本書。

+0

我認爲OP想要*排序鍵,不確定hashCode會有幫助。 – 2012-02-27 13:59:19

+1

@PeterLawrey LinkedHashSet不會對任何東西進行排序。他確實提到他受到侵犯的唯一性。在任何情況下,這本書都會談論可比性,所以閱讀它會有所幫助。 – 2012-02-27 14:02:50

+0

作爲原始列表已經排序,我不在乎目前的排序,我關心的順序。非常感謝鏈接,我現在會檢查這本書。 – AhmadAssaf 2012-02-27 14:20:40