Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

前端 未结 9 2139
无人及你
无人及你 2020-11-30 18:38

I know how to \"transform\" a simple Java List from Y -> Z, i.e.:

List x;
List y = x.stre         


        
9条回答
  •  臣服心动
    2020-11-30 18:54

    An alternative that always exists for learning purpose is to build your custom collector through Collector.of() though toMap() JDK collector here is succinct (+1 here) .

    Map newMap = givenMap.
                    entrySet().
                    stream().collect(Collector.of
                   ( ()-> new HashMap(),
                           (mutableMap,entryItem)-> mutableMap.put(entryItem.getKey(),Integer.parseInt(entryItem.getValue())),
                           (map1,map2)->{ map1.putAll(map2); return map1;}
                   ));
    

提交回复
热议问题