import java.util.HashMap; public class SizeExample { public static void main(String[] args) { HashMapmap = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); int size = map.size(); System.out.println("Size of map is: " + size); } }
import java.util.HashMap; public class ClearExample { public static void main(String[] args) { HashMapIn this example, we create a new HashMap called `map` and insert three key-value pairs into it. We then print the original map to the console, clear the map using the `clear()` method, and print the empty map to the console. Package library: java.utilmap = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); System.out.println("Original map: " + map); map.clear(); System.out.println("Cleared map: " + map); } }