Question:
How do I iterate over each Entry in a Map?
Code:
import java.util.*; public class MapExample { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>(); map.put( "String1", "aaaa" ); map.put( "String2", "bbbb" ); map.put( "String3", "cccc" ); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }
Output:
$ java MapExample String3:cccc String1:aaaa String2:bbbb