如何从最少顺序的反馈列表中找到每个用户给出的最新反馈?

反馈看起来像这样:


    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;

    }

我得到了这些反馈的列表,我想要一个反馈列表,其中只有每个用户的最新 feedbackReceivedTime。


我可以遍历反馈列表并找到唯一的用户,然后为每个用户遍历反馈列表以获得最新的反馈,但这并不是最不重要的。


呼如林
浏览 130回答 5
5回答

千巷猫影

这是使用 Stream API 执行此操作的方法:List<FeedbackTable> feedbacks = ...; // your list of feedbacksList<FeedbackTable> latestUsersFeedbacks&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = feedbacks.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toMap(f -> f.user,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f -> f,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (f1, f2) -> f1.feedbackReceivedTime.after(f2.feedbackReceivedTime) ? f1 : f2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.values().stream().collect(Collectors.toList());

米脂

您可以使用流来解决问题:List<FeedbackTable>&nbsp;rawFeedbacks; List<FeedbackTable>&nbsp;newestFeedbacks&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;rawFeedbacks &nbsp;&nbsp;&nbsp;&nbsp;.stream() &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.groupingBy(FeedbackTable::getUser)) &nbsp;&nbsp;&nbsp;&nbsp;.entrySet().stream() &nbsp;&nbsp;&nbsp;&nbsp;.map(a&nbsp;->&nbsp;a.getValue().stream().max(Comparator.comparing(FeedbackTable::getFeedbackReceivedTime)).get()) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

白猪掌柜的

如果您可以控制 FeebbackTable 类,那么最好是重写它的 equals 和 hashcode 方法并实现java.lang.Comparable#compareTo. 像这样,public class FeedbackTable implements Comparable<FeedbackTable> {&nbsp; &nbsp; private final String id;&nbsp; &nbsp; private final String user;&nbsp; &nbsp; private final int feedback;&nbsp; &nbsp; private final Date feedbackReceivedTime;&nbsp; &nbsp; private final Date eventStartTime;&nbsp; &nbsp; public FeedbackTable(&nbsp; &nbsp; &nbsp; &nbsp; String clusterKey,&nbsp; &nbsp; &nbsp; &nbsp; Date eventStartTime,&nbsp; &nbsp; &nbsp; &nbsp; String user,&nbsp; &nbsp; &nbsp; &nbsp; Date feedbackReceivedTime,&nbsp; &nbsp; &nbsp; &nbsp; int feedback) {&nbsp; &nbsp; &nbsp; this.id = clusterKey;&nbsp; &nbsp; &nbsp; this.user = user;&nbsp; &nbsp; &nbsp; this.feedback = feedback;&nbsp; &nbsp; &nbsp; this.feedbackReceivedTime = feedbackReceivedTime;&nbsp; &nbsp; &nbsp; this.eventStartTime = eventStartTime;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; if (o == null || getClass() != o.getClass()) return false;&nbsp; &nbsp; &nbsp; FeedbackTable that = (FeedbackTable) o;&nbsp; &nbsp; &nbsp; return id.equals(that.id) && user.equals(that.user);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; return Objects.hash(id, user);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(FeedbackTable that) {&nbsp; &nbsp; &nbsp; if (this.user.equals(that.user)) {&nbsp; &nbsp; &nbsp; &nbsp; if (this.feedbackReceivedTime.before(that.feedbackReceivedTime)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; } else if (this.feedbackReceivedTime.after(that.eventStartTime)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return this.user.compareTo(that.user);&nbsp; &nbsp; }}稍后,只需使用流 API,feedbackTables.stream().sorted().distinct().collect(Collectors.toList());你完成了。

小怪兽爱吃肉

您可以将 aHashMap与每个用户一起用作键并相应地更新用户的最新反馈( FeedbackTable) 值:public ArrayList<FeedbackTable> getLatestFeedbacksForUsers(List<FeedbackTable> feedbackTableList) {&nbsp; &nbsp; Map<String, FeedbackTable> latestFeedbackMap = new HashMap<>();&nbsp; &nbsp; for (FeedbackTable ft : feedbackTableList) {&nbsp; &nbsp; &nbsp; &nbsp; if (latestFeedbackMap.containsKey(ft.user)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (latestFeedbackMap.get(ft.user).feedbackReceivedTime.before(ft.feedbackReceivedTime)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latestFeedbackMap.put(ft.user, ft);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latestFeedbackMap.put(ft.user, ft);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; List<FeedbackTable> latestFeedbacks = new ArrayList<FeedbackTable>(latestFeedbackMap.values());&nbsp; &nbsp; // Sort Feedbacks in descending order&nbsp; &nbsp; Collections.sort(latestFeedbacks, new Comparator<FeedbackTable>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int compare(FeedbackTable f1, FeedbackTable f2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return f2.feedbackReceivedTime().compareTo(f1.feedbackReceivedTime());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return latestFeedbacks;}

沧海一幻觉

这不是您要找的 100%Map<String,&nbsp;Optional<FeedbackTable>>&nbsp;datesMap&nbsp;=&nbsp;tables.stream().collect( &nbsp;&nbsp;Collectors.groupingBy(&nbsp;(ft)&nbsp;->&nbsp;ft.user,&nbsp;Collectors.reducing( &nbsp;&nbsp;&nbsp;&nbsp;(ft1,&nbsp;ft2)&nbsp;->&nbsp;ft1.feedbackReceivedTime.compareTo(&nbsp;ft2.feedbackReceivedTime&nbsp;)&nbsp;>&nbsp;0&nbsp;?&nbsp;ft1&nbsp;:&nbsp;ft2&nbsp;)&nbsp;)&nbsp;);所以你需要这个来获取用户的日期:datesMap.get( "username" ).get().feedbackReceivedTime但你不需要&nbsp;getFeedbackReceivedTime()&nbsp;方法
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java