我正在尝试使用 Java7 的 WeakHashMap,但我发现它的 isEmpty() 方法给了我错误的结果。
import java.util.Map;
import java.util.WeakHashMap;
public class Test
{
public static void main(final String[] args)
{
final Map<String, Boolean> map = new WeakHashMap<>();
String b = new String("B");
map.put(b, true);
b = null;
System.gc();
System.out.println(map.isEmpty());
System.out.println(map.keySet().isEmpty());
System.out.println(map);
}
}
实际结果:
错误的
真的
{}
也就是说,
map.isEmpty() 和 map.keySet().isEmpty() 不一致。有人可以帮我理解吗?非常感谢。