Example
Retrieve all the keys contained within 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.keySet());
}
}
|
Definition and Usage
The keySet() method returns a set comprising all the keys present in the map.
Note: The set returned is a view of the map, implying that modifications made to the set will also affect the map.
Syntax
K represents the data type of the map’s keys.
Technical Details
|
Returns:
|
A set that encompasses all the keys within the map.
|