Android,从两个数组列表创建一个数组列表
我在比较两个数组列表时遇到问题,
我的第一个数组列表如下所示:
{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;
I have a problem with comparing two arraylist,
my first arraylist looks like this:
{TeamID=4, Group=A, TeamName=Germany}
{TeamID=6, Group=A, TeamName=Turkey}
my second list:
{TeamID=4, Chance=99.9%}
{TeamID=6, Chance=38.4%}
and then I want to create one list, which will look like this:
{TeamID=4, Group=A, TeamName=Germany Chance=99.9%}
{TeamID=6, Group=A, TeamName=Turkey Chance=38.4%}
Can You help me ?
First List:
private ArrayList<HashMap<String, String>> TeamList = this.xml.TeamListList;
Second:
private ArrayList<HashMap<String, String>> QChanceList = this.xml.QChanceListList;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个想法。对于每个团队,我在机会列表中搜索相应的条目,然后将两张地图中的所有条目放入新地图中。
(这个例子并不是非常安全,它断言所有地图都有“TeamId”值,我只是证明在非法输入的情况下必须采取一些措施,比如不完整的机会列表)
Here's one idea. For each team I search for the corresponding entry in the chances list, then I put all entries from both maps into a new one.
(This example is not very fail-safe, it asserts, that all maps have values for "TeamId", I just demonstrate that something has to be done in case of illegal input, like an incomplete chances list)
团队列表应该是地图的地图。使用团队 ID 作为外部地图中的键(如果您不想更改为数据库)。
合并条目将非常容易。迭代机会列表,从团队地图中获取团队,然后使用 teamMap.putAll(mapFromChanceList)
编辑:使用示例进行更新。
Team list should be a map of maps. Use team id as key in the outer map (if you don't want to change to a database).
Merging the entries would then be very easy. Iterate over chance list, get the team from the teams map, and use
teamMap.putAll(mapFromChanceList)
Edit: Update with example.
与 Andreas_D 类似,这是给猫剥皮的另一种方法。
我想说我仍然同意 Dori 的观点,这确实看起来像数据库数据。我建议您使用数据库,这样可以避免解决这个已经解决的问题的麻烦。
或者与您用来获取此数据的 API 的所有者合作,让他们在数据到达您之前使用正确的数据库查询来合并它。
放弃这一点,你最好的选择是做这样的事情来达到这一点,这样你就可以使用 hashmap 的实用程序。
Similarly to Andreas_D, here's another way to skin that cat.
I would like to say that I still agree with Dori, this really does look like database data. I'd recommend you use a database and save yourself the headache of solving this problem that's already been solved.
Either that or work with the owner of the API you're using to get this data and get them to use a proper database query to merge it before it ever gets to you.
Forgoing that, your best bet is to do something like this to get to that point so that you can use the utilities of hashmap.