Reference tricks

Report a typo

What map size will this application print?

public class WeakHashMapDemo {
    public static void main(String[] args) throws InterruptedException {
        Person joshua = new Person("Joshua Bloch", 1961);
        Person bruce = new Person("Bruce Eckel", 1957);
        Person person = joshua;

        Map<Person, String> map = new WeakHashMap();
        map.put(joshua, "Effective Java");
        map.put(bruce, "Thinking in Java"); 
        
        joshua = null;
        System.gc();
        Thread.sleep(250);

        System.out.println(map.size());
    }
}
Enter a number
___

Create a free account to access the full topic