gpt4 book ai didi

java - Android,从两个数组列表创建一个数组列表

转载 作者:行者123 更新时间:2023-11-29 22:24:29 33 4
gpt4 key购买 nike

我在比较两个数组列表时遇到了问题,

我的第一个数组列表是这样的:

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

我的第二个 list :

{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;

最佳答案

这是一个想法。对于每个团队,我在机会列表中搜索相应的条目,然后将两张 map 中的所有条目放入一个新的条目中。

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;
}

(这个例子不是很安全,它断言,所有 map 都有“TeamId”的值,我只是证明在非法输入的情况下必须做一些事情,比如不完整的机会列表)

关于java - Android,从两个数组列表创建一个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6292044/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com