javaimporthashmapentryset

Cannot find symbol when using entrySet() function after import java.util.*


I am using a function Map.entrySet(). When I am using import java.util.* it gives cannot find symbol error. But, when I am doing import java.util.Map.Entry it compiles. Shouldn't "*" include Map.Entry?

Am I missing anything?

Bottom line using import java.util.* gives me cannot find symbol error. For the same code import java.Map.Entry; does not. Why?

Thank You.


Solution

  • The star import is used for importing all classes of the package. When you specify

    import java.util.*;
    

    You are denoting that all classes that have full name java.util.<ClassName> are to be considered imported.

    java.util.Map.Entry is an inner class of java.util.Map class. The star import for sub-classes will be something like this

    import java.util.Map.*;
    

    I can't say that it is a good practice to use start imports at all. Most of coding guidelines recommend to avoid it. Sub-class star imports isn't something you usually will come across in the code.