Practice ยท 3 of 3
Weak keys, retired for real
Inside Solution, keep static class CacheKey { final String id; ... } and a
private static final WeakHashMap<CacheKey, String> store with static void put,
static String get, plus:
1. static int weakRetire() โ seed 3 key/value pairs from a PRIVATE helper (so the
key objects are reachable ONLY through the weak map once the helper returns), call
System.gc() twice, then return how many entries remain (probe store.size(), which
expunges cleared entries). Correct answer: 0.
2. static int strongRetire() โ same seeding, but ALSO retain the keys in a
static List<CacheKey> pinned; gc twice; return survivors (correct: 3).
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Memory drills