Here you can find the source of addToMapMap(Map
public static <T, V> void addToMapMap(Map<T, Map<V, Integer>> map, T key, V val)
//package com.java2s; //License from project: Apache License import java.util.HashMap; import java.util.Map; public class Main { public static <T, V> void addToMapMap(Map<T, Map<V, Integer>> map, T key, V val) { if (!map.containsKey(key)) { Map<V, Integer> newMap = new HashMap<V, Integer>(); addToMap(newMap, val); map.put(key, newMap);//from w w w . ja v a 2 s.co m } else { addToMap(map.get(key), val); } } public static <T> void addToMap(Map<T, Integer> map, T item) { if (!map.containsKey(item)) { map.put(item, 1); } else { int count = map.get(item); map.put(item, count + 1); } } }