WeakedHashMap会在key对象已经没有引用时,如果调用System.gc()会尝试remove这个key
import java.util.*;public class WeakedHashMap { public static void main(String args[]){ Mapmap = new WeakHashMap (); //Map map = new HashMap (); String alibaba = new String("TEST"); map.put(alibaba, "TEST"); while (map.containsKey("TEST")) { try { Thread.sleep(500); alibaba = null; } catch (InterruptedException ignored) { } System.out.println("Checking for empty"); System.gc(); } }}