Java tutorial
//package com.java2s; //License from project: BSD License import java.util.ArrayList; import java.util.Collection; import java.util.Map; public class Main { /** * Returns the collection value for the given key, creating and associating * a new, empty {@link ArrayList} if the key's value is null. * * @param <K> * The map's key type. * @param <V> * The map's collection value type. * @param collectionMap * The map to evaluate. * @param key * The key to fetch. * @return A collection of values for the given key. */ public static <K, V> Collection<V> initCollectionValue(final Map<K, Collection<V>> collectionMap, final K key) { Collection<V> collection = collectionMap.get(key); if (collection == null) { collection = new ArrayList<V>(); collectionMap.put(key, collection); } return collection; } }