What's the best way to sum two Map<String,String>?

只谈情不闲聊 提交于 2019-12-22 19:20:14

问题


I have the following maps.

Map<String,String> map1= new HashMap<String, String>(){{
       put("no1","123"); put("no2","5434"); put("no5","234");}};

Map<String,String> map1 = new HashMap<String, String>(){{
       put("no1","523"); put("no2","234"); put("no3","234");}};

sum(map1, map2);

I want to join them to one, summing up similar keyed values together. What;s the best way I could do it using java 7 or guava libraries ?

expected output

Map<String, String> output = { { "no1" ,"646"}, { "no2", "5668"}, {"no5","234"}, {"no3","234" }  }

回答1:


private static Map<String, String> sum(Map<String, String> map1, Map<String, String> map2) {
        Map<String, String> result = new HashMap<String, String>();
        result.putAll(map1);
        for (String key : map2.keySet()) {
            String value = result.get(key);
            if (value != null) {
                Integer newValue = Integer.valueOf(value) + Integer.valueOf(map2.get(key));
                result.put(key, newValue.toString());
            } else {
                result.put(key, map2.get(key));
            }
        }
        return result;
    }



回答2:


try this

    Map<String, List<String>> map3 = new HashMap<String, List<String>>();
    for (Entry<String, String> e : map1.entrySet()) {
        List<String> list = new ArrayList<String>();
        list.add(e.getValue());
        String v2 = map2.remove(e.getKey());
        if (v2 != null) {
            list.add(v2);
        }
        map3.put(e.getKey(), list);
    }

    for (Entry<String, String> e : map2.entrySet()) {
        map3.put(e.getKey(), new ArrayList<String>(Arrays.asList(e.getValue())));
    }



回答3:


Java 8 introduces Map.merge(K, V, BiFunction), which makes this easy if not particularly concise:

Map<String, String> result = new HashMap<>(map1);
//or just merge into map1 if mutating it is okay
map2.forEach((k, v) -> result.merge(k, v, (a, b) ->
    Integer.toString(Integer.parseInt(a) + Integer.parseInt(b))));

If you're doing this repeatedly, you're going to be parsing and creating a lot of strings. If you're generating the maps one at a time, you're best off maintaining a list of strings and only parsing and summing once.

Map<String, List<String>> deferredSum = new HashMap<>();
//for each map
mapN.forEach((k, v) ->
    deferredSum.computeIfAbsent(k, x -> new ArrayList<String>()).add(v));
//when you're done
Map<String, String> result = new HashMap<>();
deferredSum.forEach((k, v) -> result.put(k,
    Long.toString(v.stream().mapToInt(Integer::parseInt).sum())));

If this summing is a common operation, consider whether using Integer as your value type makes more sense; you can use Integer::sum as the merge function in that case, and maintaining lists of deferred sums would no longer be necessary.




回答4:


Try this

    Map<String,String> map1= new HashMap<String, String>(){{
        put("no1","123"); put("no2","5434"); put("no5","234");}};
    Map<String,String> map2 = new HashMap<String, String>(){{
        put("no1","523"); put("no2","234"); put("no3","234");}};
    Map<String,String> newMap=map1;
    for(String a:map2.keySet()){
       if(newMap.keySet().contains(a)){
               newMap.put(a,""+(Integer.parseInt(newMap.get(a))+Integer.parseInt(map2.get(a))));
       }
        else{
            newMap.put(a,map2.get(a));
        }
    }
    for(String k : newMap.keySet()){
        System.out.println("key : "+ k + " value : " + newMap.get(k));
    }


来源:https://stackoverflow.com/questions/17400732/whats-the-best-way-to-sum-two-mapstring-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!