My code works.. but this doesn't compile in Codegym.
Btw why what is the difference in line 13 between
HashMap<String, String> map = new HashMap<String, String>();
and
Map<String, String> map = new HashMap<String, String>();
?package com.codegym.task.task08.task0802;
/*
HashMap of 10 pairs
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Solution {
public static void main(String[] args) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
map.put("watermelon", "melon");
map.put("banana", "fruit");
map.put("cherry", "fruit");
map.put("pear", "fruit");
map.put("cantaloupe", "melon");
map.put("blackberry", "fruit");
map.put("ginseng", "fruit");
map.put("strawberry", "flower");
map.put("iris", "flower");
map.put("potato", "tuber");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
// Get a key-value pair
Map.Entry<String, String> pair = iterator.next();
String key = pair.getKey(); // Key
String value = pair.getValue(); // Value
System.out.println(key + " - " + value);
}
}
}