Java tutorial
//package com.java2s; import java.util.Map; public class Main { /** * 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); } } }