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

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

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

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


当前回答

此解决方案适用于Android Studio 3.0或更高版本。

文件>项目结构>模块>应用>属性选项卡

将Source Compatibility和Target Compatibility都更改为1.8

编辑配置文件

您也可以在相应的版本中直接配置它。gradle文件

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

其他回答

在IntelliJ IDEA 15中使用Java 8 (lambdas等)编译;

CTRL + ALT + SHIFT + S或文件/项目结构 打开项目选项卡 将项目SDK设置为兼容的java版本(1.8) “项目语言级别”设置为“8” 打开模块选项卡 设置“语言级别”为“8”,单击“确定” CTRL + ALT + S或文件/设置 构建,执行,部署/编译器/ Java编译器 将目标字节码版本更改为1.8,单击确定

即使在IntelliJ和Eclipse上应用了上述定义的项目特定设置,它仍然是失败的!

对我来说有用的是在POM XML中添加了带有源和目标的maven插件,设置为1.8:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.abc.sparkcore.JavaWordCount</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>   
     </execution>
            </executions>
        </plugin>
    </plugins>
</build> 

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

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

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

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

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

为lambda表达式提供Ecplise支持:

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

Ctrl + Alt + Shift + S,然后从项目选择语言级别1.8,阅读更多关于Lambda表达式在这里