WeakedHashMap 的例子

WeakedHashMap会在key对象已经没有引用时,如果调用System.gc()会尝试remove这个key
import java.util.*;

public class WeakedHashMap {
    public static void main(String args[]){
    	Map<String, String> map = new WeakHashMap<String, String>();
    	//Map<String, String> map = new HashMap<String, String>();  
    	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();   
        }  
    }
}

你可能感兴趣的:(map,WeakedHashMap)