To use collectors class methods like Collectors.toList() , we need to import java.util.stream.Collectors; I have found that Collectors is a class and not a package as an answer at some places. But I still don't understand even if Collectors is a class, it is still part of java.util.* . Then why it needs to be imported seperately.
Asked
Active
Viewed 217 times
-3
-
It's part of `java.util.stream.*`. – Kayaman Nov 13 '20 at 11:24
-
5Because, as you say, it's part of `java.util.stream`, not `java.util`. `import java.util.*` means "import all the classes directly under the `java.util` package". It's not recursive. – Federico klez Culloca Nov 13 '20 at 11:24
-
https://stackoverflow.com/questions/18245483/recursive-import-java – Reimeus Nov 13 '20 at 11:26
-
Packages in Java are not nested, even if their names suggest it. From a technical perspective `java.util.stream` is as connected to `java.util` as `java.nio` is: not at all. Just because they share a prefix doesn't mean that one "contains" the other. – Joachim Sauer Nov 13 '20 at 11:37
1 Answers
0
When you do the import of java.util.stream
package you only import classes at the directory level specified. If you want to import classes at the level below this, you need to import that package directory as well. The import call does not recursively import everything from all directories and below, it only imports what you ask for at the level specified.

Rob Evans
- 2,822
- 1
- 9
- 15