5 回答
TA贡献1829条经验 获得超7个赞
这是使用 Stream API 执行此操作的方法:
List<FeedbackTable> feedbacks = ...; // your list of feedbacks
List<FeedbackTable> latestUsersFeedbacks
= feedbacks.stream()
.collect(Collectors.toMap(f -> f.user,
f -> f,
(f1, f2) -> f1.feedbackReceivedTime.after(f2.feedbackReceivedTime) ? f1 : f2))
.values().stream().collect(Collectors.toList());
TA贡献1836条经验 获得超3个赞
您可以使用流来解决问题:
List<FeedbackTable> rawFeedbacks; List<FeedbackTable> newestFeedbacks = rawFeedbacks .stream() .collect(Collectors.groupingBy(FeedbackTable::getUser)) .entrySet().stream() .map(a -> a.getValue().stream().max(Comparator.comparing(FeedbackTable::getFeedbackReceivedTime)).get()) .collect(Collectors.toList());
TA贡献1893条经验 获得超10个赞
如果您可以控制 FeebbackTable 类,那么最好是重写它的 equals 和 hashcode 方法并实现java.lang.Comparable#compareTo. 像这样,
public class FeedbackTable implements Comparable<FeedbackTable> {
private final String id;
private final String user;
private final int feedback;
private final Date feedbackReceivedTime;
private final Date eventStartTime;
public FeedbackTable(
String clusterKey,
Date eventStartTime,
String user,
Date feedbackReceivedTime,
int feedback) {
this.id = clusterKey;
this.user = user;
this.feedback = feedback;
this.feedbackReceivedTime = feedbackReceivedTime;
this.eventStartTime = eventStartTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FeedbackTable that = (FeedbackTable) o;
return id.equals(that.id) && user.equals(that.user);
}
@Override
public int hashCode() {
return Objects.hash(id, user);
}
@Override
public int compareTo(FeedbackTable that) {
if (this.user.equals(that.user)) {
if (this.feedbackReceivedTime.before(that.feedbackReceivedTime)) {
return 1;
} else if (this.feedbackReceivedTime.after(that.eventStartTime)) {
return -1;
} else {
return 0;
}
}
return this.user.compareTo(that.user);
}
}
稍后,只需使用流 API,
feedbackTables.stream().sorted().distinct().collect(Collectors.toList());
你完成了。
TA贡献1852条经验 获得超1个赞
您可以将 aHashMap与每个用户一起用作键并相应地更新用户的最新反馈( FeedbackTable) 值:
public ArrayList<FeedbackTable> getLatestFeedbacksForUsers(List<FeedbackTable> feedbackTableList) {
Map<String, FeedbackTable> latestFeedbackMap = new HashMap<>();
for (FeedbackTable ft : feedbackTableList) {
if (latestFeedbackMap.containsKey(ft.user)) {
if (latestFeedbackMap.get(ft.user).feedbackReceivedTime.before(ft.feedbackReceivedTime)) {
latestFeedbackMap.put(ft.user, ft);
}
} else {
latestFeedbackMap.put(ft.user, ft);
}
}
List<FeedbackTable> latestFeedbacks = new ArrayList<FeedbackTable>(latestFeedbackMap.values());
// Sort Feedbacks in descending order
Collections.sort(latestFeedbacks, new Comparator<FeedbackTable>() {
@Override
public int compare(FeedbackTable f1, FeedbackTable f2) {
return f2.feedbackReceivedTime().compareTo(f1.feedbackReceivedTime());
}
});
return latestFeedbacks;
}
TA贡献1824条经验 获得超5个赞
这不是您要找的 100%
Map<String, Optional<FeedbackTable>> datesMap = tables.stream().collect( Collectors.groupingBy( (ft) -> ft.user, Collectors.reducing( (ft1, ft2) -> ft1.feedbackReceivedTime.compareTo( ft2.feedbackReceivedTime ) > 0 ? ft1 : ft2 ) ) );
所以你需要这个来获取用户的日期:datesMap.get( "username" ).get().feedbackReceivedTime
但你不需要 getFeedbackReceivedTime()
方法
添加回答
举报