Java tutorial
//package com.java2s; import java.util.Map; public class Main { /** Merge two map counters. */ public static <K> void mergeMapCounters(Map<K, Integer> counter1, Map<K, Integer> counter2) { for (K key : counter2.keySet()) { increaseMapCounter(counter1, key, counter2.get(key)); } } /** * Increase counter.get(key) by increase. * If value decrease to zero, it is removed from map. */ public static <K> void increaseMapCounter(Map<K, Integer> counter, K key, int increase) { if (!counter.containsKey(key)) { counter.put(key, 0); } counter.put(key, counter.get(key) + increase); if (counter.get(key) == 0) { counter.remove(key); } } }