foreach on maps
843793Aug 29 2003 — edited Mar 14 2006I proposed this a while ago, and as far as i remember everybody nodded, said yes, and then got on with their lives without a second thought.
To iterate through the elements in a Map with the new for loops, you have to do:
Map <String, Widget> mymap = ...;
for (Map.Entry <String, Widget> entry : mymap) {
String key = entry.getKey();
Widget value = entry.getValue();
...
}
I'd far rather this was handled with a syntax similar (but not necessarily identical) to this:
Map <String, Widget> mymap = ...;
for (String key, Widget value : mymap) {
...
}
Like i said, nobody had an objection - even the members of the Java team i spoke to - but it seems to have been forgotten about. Is there a good reason?