简体   繁体   English

比较器:此语言级别不支持方法引用吗?

[英]Comparator: Method references not supported at this language level?

I wrote the following line of code for a Java 8 project: 我为Java 8项目编写了以下代码行:

Comparator<Person> oldestPerson= Comparator.comparing(Person::getOldestBirthday);

It is a simple comparator, however I am trying to use this code In a project that only uses Java 7 , therefore giving this error: 这是一个简单的比较器,但是我试图在仅使用Java 7的项目中使用此代码,因此出现此错误:

"Method references are not supported at this language level"

I understand im getting the error as im not using Java 8. Is there a simple way to re-write the logic without having to use a method reference? 我知道由于没有使用Java 8而收到错误消息。是否有一种简单的方法可以重写逻辑而不必使用方法引用?

Comparator<Person> comparator = new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getOldestBirthday().compareTo(p2.getOldestBirthday());
    }
};

This is probably the simplest way to do it in Java 7 这可能是Java 7中最简单的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM