【问题标题】:How to put data in the map that in the another map?如何将数据放在另一个地图中的地图中?
【发布时间】:2022-01-08 20:45:28
【问题描述】:

当我尝试将案件编号添加到每个国家/地区的每个日期时,我遇到了困难。在我的尝试中,它只会添加每个国家/地区每个日期的整个记录​​列表

下面是List<List<String>> covidConfirmedList的输出

[Province/State, Country/Region, Lat, Long, 1/22/20, 1/23/20, 1/24/20, 1/25/20, 1/26/20, 1/27/20]
[, Afghanistan, 33.93911, 67.709953, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
[, Angola, -11.2027, 17.8739, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0]
[, Angeria, -12.3047, 17.8739, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0]
[, Andora, -13.2087, 17.8739, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0]

我打算存储在 Map> 中。在每个国家的每个日期,它应该只存储一条记录。

预期输出

Afghanistan --> {2020-01-22=[0], 2020-01-23=[0], 2020-01-24=[3], 2020-01-25=[0], 2020-01-26=[0]}
Angola --> {2020-01-22=[0], 2020-01-23=[0], 2020-01-24=[0], 2020-01-25=[0], 2020-01-26=[0]} 
Angeria --> {2020-01-22=[0], 2020-01-23=[0], 2020-01-24=[0], 2020-01-25=[0], 2020-01-26=[0]} 
Andora --> {2020-01-22=[0], 2020-01-23=[0], 2020-01-24=[0], 2020-01-25=[0], 2020-01-26=[0]} 

我的尝试

 Map<String, Map<LocalDate, List<Integer>>> dataMap = new LinkedHashMap<>();
 Map<LocalDate,List<Integer>> innerMap = new LinkedHashMap<>();
        IntStream //functional for loop to add the date as keys into the map
                .range(0,covidListWithoutCountryDetails.get(0).size())
                .forEach(i->
                        innerMap
                                .put(keys.get(i),new ArrayList<>()
                                )
                );
IntStream  //functional for loop to add the country keys into map 
    .range(0,mapKeys.size())
    .forEach(i->dataMap
    .put(mapKeys.get(i), innerMap));

【问题讨论】:

    标签: java linkedhashmap


    【解决方案1】:

    我看到您使用 LocalDate 作为键 - 它不会重复键吗?

    【讨论】:

    • 我使用国家作为关键字
    • Map&lt;LocalDate,List&lt;Integer&gt;&gt; innerMap = new LinkedHashMap&lt;&gt;(); 但是在这里?
    • 这是存储日期和对应的每日病例编号
    猜你喜欢
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多