What is .keySet()?


In the last part of data structures there is this "for" loop:

for (String city : majorCities.keySet()) {

		System.out.println(city + " was founded in " + majorCities.get(city));


Why is .keySet() needed? What does it do?


Method used to get a Set view of the keys contained in this HashMap.


Not just HashMap but TreeMap and LinkedHashMap etc. keySet() is a method of the Map interface so any implementing classes like the ones above has access to this method.


You obviously got your answer from the below so please link your sources