I am new to Java and writing java on a Mac using HashMap.
However I encountered a problem that I can't find the answer
import java.util.Map; import java.util.HashMap; public class Test { public static void main(String[] args) { Map<String, Integer> hm = new HashMap<>(); hm.put("a", 1); hm.put("b", 2); for (Entry<String, Integer> en : hm.entrySet()) { //this line is different System.out.print(en.getKey()); System.out.println(en.getValue()); } } } This code works fine on a windows machine, but on my Mac it pops out an error indicating that "can not find symbol: Entry "
Later I changed code to
import java.util.Map; import java.util.HashMap; public class Test { public static void main(String[] args) { Map<String, Integer> hm = new HashMap<>(); hm.put("a", 1); hm.put("b", 2); for (Map.Entry<String, Integer> en : hm.entrySet()) { //this line is different System.out.print(en.getKey()); System.out.println(en.getValue()); } } } And now it works fine.
Can anybody tell me why?
Why this code works fine on others computer but not on mine?
Entry.Entry, or you can importMapand writeMap.Entry, or you can import neither and writejava.util.Map.Entry