Read HashMap using Iterator in Java
Here I have used LinkedHashMap since I needed a map in insertion order. No matter it is LinkedHashMap or just HashMap this is the way to iterate it and read elements of key values pairs.
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("A", "1");
map.put("M", "1M");
map.put("B.C", "2");
map.put("B.D.e", "2");
map.put("B.D.H", "2");
Iterator<Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry)it.next();
System.out.println("Key - " + entry.getKey().toString());
System.out.println("Value - " + entry.getValue().toString());
}
Do not hesitate to contact me if you have any issue. This is my tested code and it works fine. Happy coding.
You comments and ideas are highly admirable.
Comments
Post a Comment