O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Iterating Over a Collection

// For a set or list
											// collection is the set or list object
											for (Iterator it=collection.iterator();
											it.hasNext(); ) {
											Object element = it.next();
											}
											// For keys of a map
											for (Iterator it =map.keySet().iterator();
											it.hasNext(); ) {
											Object key = it.next();
											}
											// For values of a map
											for (Iterator it =map.values().iterator();
											it.hasNext(); ) {
											Object value = it.next();
											}
											// For both the keys and values of a map
											for (Iterator it =map.entrySet().iterator();
											it.hasNext(); ) {
											Map.Entry entry = (Map.Entry)it.next();
											Object key = entry.getKey();
											Object value = entry.getValue();
											}

The java.util package contains an Iterator class that makes iterating over a collection a relatively simple task. To iterate over a collection object, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required