5 回答

TA貢獻1829條經(jīng)驗 獲得超7個贊
這是使用 Stream API 執(zhí)行此操作的方法:
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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超10個贊
如果您可以控制 FeebbackTable 類,那么最好是重寫它的 equals 和 hashcode 方法并實現(xiàn)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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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()
方法
添加回答
舉報