Created
May 24, 2019 16:50
-
-
Save Bill/0f6a20dbaec6e9974945aa43f95f95a9 to your computer and use it in GitHub Desktop.
Using JSR 201 enhanced for loop to iterate over Map values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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