0

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?

5
  • 1
    Because you didn't import Entry. Commented Feb 13, 2015 at 0:52
  • In the second set of code, you are essentially telling the compiler that the iterations are for 'java.util.Map.Entry<String, Integer>' the first entry does not specify. Are you doing this in an IDE? The first would probably work if you also imported 'java.util.Map.Entry' Commented Feb 13, 2015 at 0:54
  • @immibis Entry is an inner class of interface Map, so not needed to import it. Commented Feb 13, 2015 at 1:06
  • 2
    @hiren You can either import Entry, or you can import Map and write Map.Entry, or you can import neither and write java.util.Map.Entry Commented Feb 13, 2015 at 1:25
  • @hiren Entry is an inner interface, those are automatically static - so you'll have to import them. Commented Feb 13, 2015 at 1:28

2 Answers 2

2

The code that you provided does not compile on windows as well. Not sure you may be compiling something else. Things can happen if you are using console and trying to compile and you have similar file names.

Map.Entry resides in java.util.Map package. So, my suggestion would be

1. You can import java.util.Map.Entry in your code.
OR
2. Use Map.Entry instead of just Entry, something like:

for (Map.Entry<String, Integer> en : hm.entrySet()) 

Basically when you use Map.Entry you are directly referencing to that class. Java's import statement is pure syntactical sugar. import is only evaluated at compile time to indicate to the compiler where to find the names in the code.

You may live without any import statement when you always specify the full qualified name of classes. Like this line needs no import statement at all:

javax.swing.JButton but = new javax.swing.JButton(); 

The import statement will make your code more readable like this:

import javax.swing.* JButton but = new JButton(); 

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks JustAditya, I was using an IDE on Win. now I know where I got wrong~
1

Are you sure this compiles in windows?

I just put this in a file called "C:\tmp\etc\Test.java" then did this...

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Bret>cd \tmp\etc C:\tmp\etc>javac Test.java Test.java:9: error: cannot find symbol for (Entry<String, Integer> en : hm.entrySet()) { //this line is different ^ symbol: class Entry location: class Test 1 error C:\tmp\etc> 

You must be doing something else that causes this...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.