Nuñ*_*ada 2 collections icomparable comparator java-8 java-stream
我有这个对象:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想按收到的日期订购;
matchService
.findAllByDay(today)
.sorted(Comparator.comparing(MatchEvent::dateReceived))
Run Code Online (Sandbox Code Playgroud)
但似乎这是不可能的,因为我遇到了编译错误:
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
Run Code Online (Sandbox Code Playgroud)
声明一个名为getDateReceived()inside的公共方法class MatchEvent,如下所示:
public Instant getDateReceived(){
return dateReceived;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用这个方法作为方法参考,如下所示:
Comparator.comparing(MatchEvent::getDateReceived)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2566 次 |
| 最近记录: |