What is .keySet()?


#1

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?
Thanks


#2

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


#3

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.


#4

You obviously got your answer from the below so please link your sources
http://www.tutorialspoint.com/java/util/hashmap_keyset.htm