I'm trying to use Java7's WeakHashMap and I found its isEmpty() method give me wrong results.
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);
}
}
The actual result:
false
true
{}
That is to say,
map.isEmpty() and map.keySet().isEmpty() is not consistent. Can someone help me to understand it? Thanks a lot.