【问题标题】:Cannot recognize java 8 lambda but the configuration is ok无法识别 java 8 lambda 但配置正常
【发布时间】:2016-06-14 21:13:59
【问题描述】:

我在 Intellij 中使用 Spring 和 Hibernate 以及所有 Java 8 的东西导入了一个程序,但我遇到了一个问题,即看不到 Java 8。 JDK设置好了,导入就OK了;我真的不明白为什么我的 IDE 用红色强调了这段代码。代码是:

@Entity
@Table(name = "author")
public class Author extends BaseEntity<Long> {

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
    private List<Book> bookList;

    public Author() {
    }

    public Author(String name, List<Book> bookList) {
        this.name = name;
        this.bookList = bookList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public Set<Book> getBooks() { // !!!!! here lines 1
        return Collections.unmodifiableSet( // 2
                this.bookList.stream(). // 3
                        collect(Collectors.toSet()));//4 are red
    }

还有import 声明:

import java.util.stream.Collectors;

也是红色的。当我点击错误时,它说:

Incompatible types.Required set <packet.model.book> 
                  Found set <java.lang.Object>

第一方面可能是什么问题?

【问题讨论】:

    标签: java spring hibernate intellij-idea


    【解决方案1】:

    在项目结构ctrl+alt+sFile -&gt; Project Structure中设置java 8

    看下图

    【讨论】:

      【解决方案2】:

      我猜你用的是 maven,对吧?如果是,请检查pom.xml中定义的java版本,如果没有定义,IDE使用的默认值可能与1.8不同。

      您可以使用maven-compiler-plugin并将jdk版本显式设置为1.8

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <showWarnings>true</showWarnings>
              <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
      </plugin>
      

      请确保您也将语言级别设置为 Java 8。如果您进入 文件 -> 项目结构 -> 项目设置 -> 项目,您可以同时配置项目 SDK 和项目那里的语言水平。确保两者都设置为 Java 8。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-09
        • 2016-03-18
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 1970-01-01
        相关资源
        最近更新 更多