Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using JSR 201 enhanced for loop to iterate over Map values
import java.util.HashMap;
import java.util.Map;
class Scratch {
interface Pool {
void basicDestroy(boolean keepAlive);
}
static class PoolImpl implements Pool {
@Override
public void basicDestroy(final boolean keepAlive) {
System.out.println("destroyed!");
}
}
public static void main(String[] args) {
final Map<String, Pool> pools = new HashMap<>();
pools.put("First", new PoolImpl());
final boolean keepAlive = true;
// Introduced in Java 1.5 (JSR 201) in 2004 (15 years ago), I give you...the ENHANCED for loop:
for (final Pool pool:pools.values()) {
pool.basicDestroy(keepAlive);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.