首页 > 解决方案 > 将地图的地图转换为java中的地图列表

问题描述

我有一个java地图,

Map<Date, Map<String, Integer>> dateTeamCountMap;

我想转换上面给出值的地图

{28-09-2018={India=14,Australia=16}, 31-09-2018={India=13,Australia=11}}

进入格式对象列表

{{Date:28-09-2018, India:14, Australia:26},{Date:31-09-2018, India:13, Australia:11}}

对不起,我是stackoverflow的新手。如果我错了,请纠正我的问题。谢谢。

我无法创建一个通用列表,因为我总是不知道团队的数量。

标签: javalistmapsjava-stream

解决方案


这样的事情怎么样?

ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
dateTeamCountMap.forEach((date, nestedMap) -> {
    Map<String, String> temp = new HashMap<String, String>();
    temp.put("Date",date);
    nestedMap.forEach((team, count) -> {
        temp.put(team,count);
    });
    list.add(temp);
});

如果您有一个代表类{Date:28-09-2018, India:14, Australia:26},那么您也可以创建List<YourClass>


推荐阅读