import java.io.*; import java.util.*; public class Example { public static void main(String[] args) { Mapmap = new HashMap (); System.out.println("Is map empty? " + map.isEmpty()); map.put("key1", "value1"); System.out.println("Is map empty? " + map.isEmpty()); } }
Is map empty? true Is map empty? false
import java.io.*; import java.util.*; public class Example { public static void main(String[] args) { Mapmap = new LinkedHashMap (); System.out.println("Is map empty? " + map.isEmpty()); map.put("key1", "value1"); System.out.println("Is map empty? " + map.isEmpty()); } }
Is map empty? true Is map empty? falseIn this example, we use a `LinkedHashMap` instead of a `HashMap`. The behavior is the same as the previous example. The `isEmpty` method is part of the `java.util` package.