I have a question about iterate a hashmap:
Map<String, Integer> map = new HashMap<String, Integer>();
I want to iterate this hashmap and printout out the key and value pair using the following code:
Iterator iterator = portMap.keySet().iterator();
but it indicates me to cast the portMap.keySet().iterator();
to (Iterator)portMap.keySet().iterator()
;, but the iterator()
return as a iterator already, why I need to cast it? And I checked some other code, there is no cast for the return value of iterator()
, what is the reason, and how to fix it?