WeakHashMap in action

Report a typo

Remove a strong reference from the author1 variable object and run garbage collection via a System.gc(). In the end, the size() method must return 1.

Sample Input 1:

Joshua Bloch
Bruce Eckel
Effective Java
Thinking in Java

Sample Output 1:

Your map size is 1. The only value is Thinking in Java
Write a program in Java 17
import java.util.Scanner;
import java.util.Map;
import java.util.WeakHashMap;

public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
String author1 = new String(scanner.nextLine());
String author2 = new String(scanner.nextLine());

Map<String, String> map = ... // inititalize an instance of WeakHashMap
map.put(author1, scanner.nextLine());
map.put(author2, scanner.nextLine());

// write your code here

System.out.printf("Your map size is %d. The only value is %s", map.size(), map.get(author2));
}
}
___

Create a free account to access the full topic