我有这个对象:
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
答案 0 :(得分:4)
在getDateReceived()
内声明一个名为class MatchEvent
的公用方法,如下所示:
public Instant getDateReceived(){
return dateReceived;
}
然后您可以将该方法用作方法参考,如下所示:
Comparator.comparing(MatchEvent::getDateReceived)