Java HashMap isEmpty() Method
Example
Check if a map is empty:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
System.out.println(capitalCities.isEmpty());
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.isEmpty());
}
}
Definition and Usage
The isEmpty()
method returns true
if the map has no entries and false
otherwise.
Syntax
public boolean isEmpty()
Technical Details
Returns: | true if the map has no entries and false otherwise. |
---|
Related Pages
❮ HashMap Methods