Java tutorial
//package com.java2s; import java.util.Collection; import java.util.Map; public class Main { /** * Add {@code value} to collection {@code map}.get({@code key}). If {@code key} does not exists * yet, set it to {@code emptyCollection} before addition. */ public static <K, V extends Collection<E>, E> void upsert(Map<K, V> map, K key, E value, V emptyCollection) { V collection = map.get(key); if (collection == null) { collection = emptyCollection; } collection.add(value); map.put(key, collection); } }