Do Java Lambda Expressions Utilize “Hidden” or Local Package Imports?

前端 未结 4 1115
一生所求
一生所求 2020-11-30 14:14

This question is about the apparent \"hidden\" or local imports of Java packages that lambda expressions seem to employ.

The following sample code compiles and runs

4条回答
  •  盖世英雄少女心
    2020-11-30 14:37

    I think the point you're trying to illustrate can be simplified like this:

    This lambda requires an import

    Paths.get("path").forEach((Path filePath) -> {});

    This lambda does not require an import

    Paths.get("path").forEach((filePath) -> {});

    Since Path.forEach(...) takes a Consumer I presume the latter case is creating a new type on-the-fly of ? super Path, and thus you don't need an import since it's a new type (like a generic type at runtime)

提交回复
热议问题