Java tutorial
//package com.java2s; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Main { public static <K, V> Map<K, V> copyNullSafeHashMap(Map<? extends K, ? extends V> map) { if (map.isEmpty()) { return Collections.emptyMap(); } return Collections.unmodifiableMap(copyNullSafeMutableHashMap(map)); } 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; } }