What’s the difference between import java.util.*; and import java.util.stream;?

I doubt that the second attempt (import java.util.stream;) works. As JonSkeet pointed out in their comment, it should result in a compilation error: error: cannot find symbol. Maybe you wanted to import java.util.stream.*;?

To the actual question:

If we import with a wildcard, that is the asterisk (*) character, only the direct classes in this package will be imported, not the classes in sub-packages. Thus with an import java.util.*, we import classes like ArrayListLinkedList and Random. A full list can be found here. The class Stream actually resides in the sub-package java.util.stream and is not imported when you import java.util.*;.

To import Stream, we can either import java.util.stream.*; (all classes within this package) or only import java.util.stream.Stream; (the FQDN of the class we need).

Leave a Comment