2011-06-09 34 views
1

我有比較兩個ArrayList的問題創建一個ArrayList中,機器人,從兩個的ArrayList

我的第一個數組列表如下:

{TeamID=4, Group=A, TeamName=Germany} 
{TeamID=6, Group=A, TeamName=Turkey} 

我的第二個列表:

{TeamID=4, Chance=99.9%} 
{TeamID=6, Chance=38.4%} 

和那麼我想創建一個列表,如下所示:

{TeamID=4, Group=A, TeamName=Germany Chance=99.9%} 
{TeamID=6, Group=A, TeamName=Turkey Chance=38.4%} 

你能幫我嗎?

第一個列表:

private ArrayList<HashMap<String, String>> TeamList = this.xml.TeamListList; 

二:

private ArrayList<HashMap<String, String>> QChanceList = this.xml.QChanceListList; 
+0

什麼在你的數組列表中?字符串?對象? – Atreys 2011-06-09 11:33:31

+1

看起來像使用teamID作爲主鍵的SQL連接 - 取決於數據/應用程序的大小和性質,您可能想要考慮使用SQLLite – Dori 2011-06-09 11:43:22

+0

我不想使用SqlLite – 2011-06-09 11:46:04

回答

1

這裏有一個想法。對於每個團隊,我都會搜索機會列表中的相應條目,然後將兩個地圖中的所有條目都放入一個新條目中。

public static List<Map<String, String>> merge(
     List<Map<String, String>> teams, List<Map<String, String>> chances) { 

    // create the result 
    List<Map<String, String>> result = new ArrayList<Map<String, String>>(); 

    // now we assume that for each team there is one chance (valid?) 
    for (Map<String, String> team:teams) { 
    boolean success = false; 
    Map<String, String> combined = new HashMap<String, String>(); 
    combined.putAll(team); 

    String id = team.get("TeamID"); 

    // now we have to find the "chance" map 
    for (Map<String, String> chance:chances) { 
     if (chance.get("TeamID").equals(id)) { 
      combined.putAll(chance); 
      boolean success = true; 
      break; 
     } 
    } 

    if (!success) { 
     // there was no entry in chances map with this id!! -> handle problem 
    } 
    result.add(combined); 
    } 
    return result; 
} 

(這個例子不是很萬無一失,它聲稱,所有的地圖有「TeamId」的價值觀,我只是表明,一些具有非法輸入的情況下要完成,像一個不完整的機會列表)

1

團隊名單應該是地圖的地圖。在外部地圖中使用團隊ID作爲鍵(如果您不想更改爲數據庫)。

合併條目將非常簡單。遍歷機會列表,從團隊地圖中獲取團隊,並使用teamMap.putAll(mapFromChanceList)

編輯:以示例更新。

Map<String, Map<String, String> teams = new HashMap<String, Map<String, String>(); 
Map<String, String> team = new Map<String, String>(); 
//populate the team map, and with TeamID, TeamName etc, then do something like this. 
teams.put(team.get("TeamID"), team); 


//You get a team by doing: 
Map<String, String> team = teams.get(teamId); //where teamId is e.g. "4" 
+0

thx爲答覆,你能告訴我,我怎樣才能創建與地圖的地圖的關鍵? – 2011-06-09 12:13:24

+0

@Grins看到我的更新回答 – Kaj 2011-06-09 12:25:04

+0

thx,我還有一個問題,我可以把這個地圖放入ListView嗎? – 2011-06-09 12:55:08

0

類似於Andreas_D,這裏是另一種皮膚的方式貓。

我想說,我仍然同意Dori,這確實看起來像數據庫數據。我建議你使用一個數據庫,併爲解決這個已經解決的問題解決頭痛問題。

要麼與您使用的API的所有者合作獲取此數據,並讓他們使用正確的數據庫查詢在它到達您之前將其合併。爲了達到這個目的,你最好的辦法就是做這樣的事情,以便你可以使用hashmap的工具。

.... 
HashMap<String, HashMap<String, String>> unifiedMap = new HashMap<String, HashMap<String, String>>(); 
for (HashMap<String, String> teamMap : teamList) { 
    String teamId = teamMap.get("TeamID"); 
    HashMap<String, String> mapFromUnified = unifiedMap.get(teamId); 
    if (mapFromUnified == null) { 
     unifiedMap.put(teamId, teamMap); 
    } else { 
     for (String key : teamMap.keySet()) { 
      // this will be a race condition, the last one in wins - good luck! 
      mapFromUnified.put(key, teamMap.get(key)); 
     } 
    } 
} 

for (HashMap<String, String> teamMap : chanceList) { 
    String teamId = teamMap.get("TeamID"); 
    HashMap<String, String> mapFromUnified = unifiedMap.get(teamId); 
    if (mapFromUnified == null) { 
     unifiedMap.put(teamId, teamMap); 
    } else { 
     for (String key : teamMap.keySet()) { 
      // this will be a race condition, the last one in wins - good luck! 
      mapFromUnified.put(key, teamMap.get(key)); 
     } 
    } 
} 
....