Java tutorial
//package com.java2s; import java.util.Map; public class Main { /** Indicates whether collection elements should be actually checked. */ private static final boolean DEBUG = true; @SuppressWarnings("unchecked") public static <K, V> Map<K, V> checkMap(Map<?, ?> map, Class<K> keyType, Class<V> valueType) { if (DEBUG) { for (Map.Entry<?, ?> entry : map.entrySet()) { keyType.cast(entry.getKey()); valueType.cast(entry.getValue()); } } return (Map<K, V>) map; } }