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.
WeakHashMap
WeakHashMap in action
Report a typo
Sample Input 1:
Joshua Bloch
Bruce Eckel
Effective Java
Thinking in JavaSample Output 1:
Your map size is 1. The only value is Thinking in JavaWrite 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));
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.