Here you can find the source of addToMap(Map
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <K, V extends Collection<T>, T> void addToMap(Map<K, V> data, K key, T value, Class<? extends Collection> clz)
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Map; public class Main { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <K, V extends Collection<T>, T> void addToMap(Map<K, V> data, K key, T value, Class<? extends Collection> clz) { V values = data.get(key);//from w w w. j a v a 2s. c o m if (values == null) { try { values = (V) clz.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException("Failed to create collection class", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Failed to create collection class", e); } data.put(key, values); } values.add(value); } }