Njax3SmmM2x2a0Zf7Hpd
Njax3SmmM2x2a0Zf7Hpd

Reputation: 1364

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

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" }  }

Upvotes: 2

Views: 3050

Answers (4)

Jeffrey Bosboom
Jeffrey Bosboom

Reputation: 13653

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.

Upvotes: 2

shem
shem

Reputation: 4712

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;
    }

Upvotes: 3

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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));
    }

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136042

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())));
    }

Upvotes: 2

Related Questions