Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static <K, V> Map<K, List<V>> copyNullSafeMultiHashMapReified(Class<V> valueType, Map<? extends K, List<?>> map) { if (valueType == null) throw new NullPointerException("valueType"); if (map == null) throw new NullPointerException("map"); @SuppressWarnings("unchecked") Map<K, List<V>> result = (Map<K, List<V>>) (Map<?, ?>) copyNullSafeMutableHashMap(map); for (Map.Entry<K, List<V>> entry : result.entrySet()) { List<V> value = entry.getValue(); ArrayList<V> valueCopy = new ArrayList<V>(value); for (V element : valueCopy) { valueType.cast(element); } entry.setValue(Collections.unmodifiableList(valueCopy)); } return result; } public static <K, V> Map<K, V> copyNullSafeMutableHashMap(Map<? extends K, ? extends V> map) { if (map == null) throw new NullPointerException("map"); Map<K, V> result = new HashMap<K, V>(map); for (Map.Entry<K, V> entry : result.entrySet()) { if (entry.getKey() == null) throw new NullPointerException("entry.getKey()"); if (entry.getValue() == null) throw new NullPointerException("entry.getValue()"); } return result; } }