gpt4 book ai didi

collections - 比较 Java8 中的 Instants

转载 作者:行者123 更新时间:2023-12-02 17:03:25 26 4
gpt4 key购买 nike

我有这个对象:

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)

关于collections - 比较 Java8 中的 Instants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52903203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com