番石榴.按标准查找Collection中的元素

gst*_*low 2 java collections guava

我有这个代码.

CommentModel lastUserComment = comments.iterator().next();
        for (CommentModel comment : comments) {
            if (comment.getCreationtime().after(lastUserComment.getCreationtime())) {
                lastUserComment = comment;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我想用番石榴替换它.

如果getCreationtime()返回int,我可以使用这样的东西: 如何从Guava中的List获取max()元素

是否有Guava工具可以解决我的问题?

Tom*_*lst 6

你可以比较时间.

final Ordering<CommentModel> o = new Ordering<CommentModel>() {
    @Override
    public int compare(final CommentModel left, final CommentModel right) {
        return left.getCreationTime().compareTo(right.getCreationTime());
    }
};
return o.max(comments);
Run Code Online (Sandbox Code Playgroud)