Example
Retrieve the number of key-value pairs in a map.
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.size());
}
}
|
Definition and Usage
The size() method returns the count of key-value pairs in a map.
Syntax
Technical Details
|
Returns:
|
The total count of key-value pairs in the map.
|