Java Map Grouping

时间:2017-07-11 12:48:51

标签: java hashmap

我有一个带有值的下列地图,我想根据键的总和部分对地图值进行分组,如

map.put("20160710-20160711 T0_20160712-20160713 T0",0.8584570960451277);
map.put("20160710-20160711 T0_20160714-20160715 T0",0.500990414391463);
map.put("20160710-20160711 T0_20160716-20160717 T1",0.5020244439749132);
map.put("20160710-20160711 T0_20160718-20160719 T1",0.43098357500551543);
map.put("20160710-20160711 T0_20160720-20160721 T4",0.5153793685149496);
map.put("20160710-20160711 T0_20160722-20160723 T3",0.4623046631067317);
map.put("20160710-20160711 T0_20160724-20160725 T0",0.4512717518558771);
map.put("20160710-20160711 T0_20160726-20160727 T4",0.36536108872340967);
map.put("20160710-20160711 T0_20160728-20160729 T1",0.3912641598156222);
map.put("20160710-20160711 T0_20160730-20160731 T4",0.28587189421297915);

map.put("20160710-20160711 T1_20160712-20160713 T1",0.8580146171263094);
map.put("20160710-20160711 T1_20160714-20160715 T1",0.9332823713689552);
map.put("20160710-20160711 T1_20160716-20160717 T0",0.71982167666725);
map.put("20160710-20160711 T1_20160718-20160719 T2",0.8699615471928571);
map.put("20160710-20160711 T1_20160720-20160721 T2",0.9278777878143865);
map.put("20160710-20160711 T1_20160722-20160723 T2",0.9344990001901656);
map.put("20160710-20160711 T1_20160724-20160725 T3",0.9400899408159816);
map.put("20160710-20160711 T1_20160726-20160727 T0",0.8886183195622867);
map.put("20160710-20160711 T1_20160728-20160729 T2",0.8298196046334939);
map.put("20160710-20160711 T1_20160730-20160731 T1",0.8409421997212592);

map.put("20160710-20160711 T2_20160712-20160713 T3",0.6215375875768216);
map.put("20160710-20160711 T2_20160714-20160715 T2",0.6464517487397089);
map.put("20160710-20160711 T2_20160716-20160717 T0",0.18854087266412126);
map.put("20160710-20160711 T2_20160718-20160719 T0",0.7104484324839435);
map.put("20160710-20160711 T2_20160720-20160721 T2",0.2253283933151363);
map.put("20160710-20160711 T2_20160722-20160723 T4",0.6041580606290883);
map.put("20160710-20160711 T2_20160724-20160725 T2",0.41264702138230575);
map.put("20160710-20160711 T2_20160726-20160727 T1",0.34820930964241165);
map.put("20160710-20160711 T2_20160728-20160729 T4",0.6120977235710975);
map.put("20160710-20160711 T2_20160730-20160731 T1",0.12926293940300582);

首先,我想为每个唯一的keys.substring(0,17)创建一个名为的文件夹 然后我想在这个文件夹中创建一个data.csv文件,其中包含keys.substring(21,38)及其值。

作为一个例子,假设我们处于循环中;

表示i = 0;

我将创建一个名为20160710-20160711 T0的文件夹,然后在此文件夹中我将创建data.csv文件。然后到每一行,我将写下该文件;

20160712-20160713 T0,0.8584570960451277 20160714-20160715 T0,0.500990414391463 20160716-20160717 T1,0.5020244439749132 20160718-20160719 T1,0.43098357500551543 20160720-20160721 T4,0.5153793685149496 20160722-20160723 T3,0.4623046631067317 20160724-20160725 T0,0.4512717518558771 20160726-20160727 T4,0.36536108872340967 20160728-20160729 T1,0.3912641598156222 20160730-20160731 T4,0.28587189421297915

对于i = 1我将创建名称为20160710-20160711 T1的文件夹然后我将在其中创建一个data.csv并在其中写入以下值;

20160712-20160713 T1",0.8580146171263094 20160714-20160715 T1",0.9332823713689552 20160716-20160717 T0",0.71982167666725 20160718-20160719 T2",0.8699615471928571 20160720-20160721 T2",0.9278777878143865 20160722-20160723 T2",0.9344990001901656 20160724-20160725 T3",0.9400899408159816 20160726-20160727 T0",0.8886183195622867 20160728-20160729 T2",0.8298196046334939 20160730-20160731 T1",0.8409421997212592

等等。 它应该像这样继续..我的问题是我如何在java地图中解析和分组键,(只是分组地图足以让我创建文件和写入无关紧要。) 非常感谢您的关心和诚意,以及最诚挚的问候。

2 个答案:

答案 0 :(得分:0)

以下是我过去通过Map进行分组的代码片段 希望这对你有好处

   Map<String, Map<String, Double>> output = new HashMap<>();
   for (String key : map.keySet()) {
        Map m = output.get(key.substring(0, 17));
        if (!output.containsKey(key.substring(0, 17)))
            m = new HashMap<>();
        output.get(key.substring(0, 17)).put(key.substring(17, 21), map.get(key))
   }

答案 1 :(得分:0)

您需要的代码是:

Map<String, List<Entry<String, String>>> result = 
  map.entrySet().stream().collect(Collectors.groupingBy(e->e.getKey().substring(0,17)));

这会将地图转换为流,然后将其转换为从字符串到条目列表的地图。如果您愿意,也可以通过在收集之前向流中添加更多调用来过滤或映射条目。