比较Java8中的Instants

问题描述 投票:1回答:1

我有这个对象:

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
collections java-8 java-stream comparator icomparable
1个回答
4
投票

getDateReceived()中声明一个名为class MatchEvent的公共方法,如下所示:

public Instant getDateReceived(){
    return dateReceived;
}

然后您可以使用此方法作为方法参考,如下所示:

Comparator.comparing(MatchEvent::getDateReceived)
© www.soinside.com 2019 - 2024. All rights reserved.