I am in the process of updating some old Java code I havent touched in a while and have a quick question about the following code snippet:
private Map<String, Example> examples = new ConcurrentHashMap<String, Example>();
...
public void testMethod() {
Enumeration allExamples = examples.elements();
while (allExamples.hasMoreElements()){
//get the next example
Example eg = (Example) allExamples.nextElement();
eg.doSomething();
}
It had previously used a hashtable, but I have replaced that with a thread safe hash map. My question is, what is the best way to iterate through a hashmap? as Enumeration has been deprecated. Should I just use a for each loop?
Any advice would be greatly appreciated.