Iterating Hashmap in Java

This code snippet shows you how to loop or iterate a Map or HashMap in Java.

Map<String, String> map = new HashMap<String, String>();
map.put("1", "Jan");
map.put("2", "Feb");
map.put("3", "Mar");

//loop a Map
for (Map.Entry<String, String> entry : map.entrySet()) {
 System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}

//Java 8 only, forEach and Lambda
map.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v));

No comments:

Post a Comment

How to handle categorical features with spark-ml?

How to Handle Categorical Features with Spark ML Categorical features are a type of feature that can take on a limited number of values, suc...