Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.Map; public class Main { /** * Decrements the integer value of the given key. In case the key does not * exists, minus one is associated with the given key. * * @param <T> The type of the keys of the given map. * @param map The given map to decrement the given key of. * @param key The given key. * @return The new value associated with the given key. */ public static <T> int decrementKey(Map<T, Integer> map, T key) { return decrementKey(map, key, 0x01); } /** * Decrements the integer value of the given key by the given decrement. In * case the given key does not exists, the key is associated with minus the * given key. * * @param <T> The type of the keys of the given map. * @param map The given map to decrement the given key of. * @param key The given key. * @param decrement The value to decrement the value associated to the given * key with. * @return The new value associated with the given key. */ public static <T> int decrementKey(Map<T, Integer> map, T key, int decrement) { return decrementKey(map, key, decrement, -decrement); } /** * Decrements the integer value of the given key by the given increment. In * case the given key does not exists, the key is associated with the given * default value. * * @param <T> The type of the keys of the given map. * @param map The given map to decrement the given key of. * @param key The given key. * @param decrement The value to decrement the value associated to the given * key with. * @param deflt The value to associate the given key with, in case the key * is not yet added to the map. * @return The new value associated with the given key. */ public static <T> int decrementKey(Map<T, Integer> map, T key, int decrement, int deflt) { Integer val = map.get(key); if (val == null) { val = deflt; } else { val -= decrement; } map.put(key, val); return val; } }