我正在测试Java 8的一些新特性,并将示例复制到我的IDE(最初是Eclipse,然后是IntelliJ),如下所示

Eclipse对lambda表达式不提供任何支持,IntelliJ不断报告错误

此语言级别不支持Lambda表达式

我想知道这是否是我的安装、代码或支持的问题。


当前回答

在应用程序级别构建中添加以下行。gradle在Android项目中解决了问题。

 compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

其他回答

使用: IntelliJ IDEA 2017.1.4 Build #IU-171.4694.23

通过项目设置/项目选项卡改变语言级别没有任何作用。相反,点击Alt + Enter并选择“设置语言级别为8 - Lambdas,类型注释等”,它会自动解决。

为lambda表达式提供Ecplise支持:

确保您使用的是JDK 1.8 (windows > Preferences > Installed JREs and Compiler); 如果您定义了任何新的函数接口,请使用注释@FunctionalInterface,这样eclipse就会建议您重构与Lambdas(函数接口)相关的结构。

这个答案对新更新的intelliJ不起作用…为了新智能…

进入——> file——> Project Structure——> Global Libraries ==>设置为JDK 1.8或更高版本。

also File——> project Structure——> projects ===> set "project language manual" = 8

——>项目结构——>项目sdk ==>

这将为您的项目启用lambda表达式。

在应用程序级别构建中添加以下行。gradle在Android项目中解决了问题。

 compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

7.0不支持lambda表达式。只需将此添加到您的应用程序gradle,将您的语言级别更改为8.0:

compileOptions {
    targetCompatibility 1.8
    sourceCompatibility 1.8
}