Glad you solved your problem, but for others entrySet() across this, if you want to iterate over entrySet() , there are two ways to do this. The first way to do this is:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class Testing { public static void main(String[] args) { Map<String, String> strMap = new HashMap<String, String>(); strMap.put("foo", "bar"); strMap.put("alpha", "beta"); for (Iterator<Entry<String, String>> iter = strMap.entrySet().iterator(); iter.hasNext(); ) { Entry<String, String> entry = iter.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } } }
Note the import at the top of the code; you probably don't see it for Iterator .
And the second:
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class Testing { public static void main(String[] args) { Map<String, String> strMap = new HashMap<String, String>(); strMap.put("foo", "bar"); strMap.put("alpha", "beta"); for (Entry<String, String> entry : strMap.entrySet()) System.out.println(entry.getKey() + "=" + entry.getValue()); } }
This is called for each loop and eliminates the need to use Iterator in general and makes the code much simpler. Note that this can also be used for arrays to remove the need for an index:
String[] strs = {"foo", "bar"}; for (String str : strs) System.out.println(str);
Brian source share