简体   繁体   中英

Comparator: Method references not supported at this language level?

I wrote the following line of code for a Java 8 project:

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:

"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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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