比较 Java8 中的 Instants

comparing Instants in Java8

我有这个对象:

public class MatchEvent implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    private Instant dateReceived;

    public Instant getDateReceived() {
        return dateReceived;
    }


    public void setDateReceived(Instant dateReceived) {
        this.dateReceived = dateReceived;
    }

}

我想按收到日期订购;

matchService
            .findAllByDay(today)
                .sorted(Comparator.comparing(MatchEvent::dateReceived))

但似乎这是不可能的,因为我遇到了一个编译错误:

Multiple markers at this line
    - The method comparing(Function<? super T,? extends U>) in the type Comparator is not applicable for the arguments 
     (MatchEvent::dateReceived)
    - The type MatchEvent does not define dateReceived(T) that is applicable here

class MatchEvent中声明一个名为getDateReceived()的public方法如下:

public Instant getDateReceived(){
    return dateReceived;
}

那么您可以将此方法作为方法参考如下:

Comparator.comparing(MatchEvent::getDateReceived)