2

I have this code.

CommentModel lastUserComment = comments.iterator().next();
        for (CommentModel comment : comments) {
            if (comment.getCreationtime().after(lastUserComment.getCreationtime())) {
                lastUserComment = comment;
            }
        }

I want to replace it using guava.

If getCreationtime() returned int I could to use something like this: How to get max() element from List in Guava

Are there in Guava tool for resolving my problem?

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

6

You can just compare the times.

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);
Tom Verelst
  • 15,324
  • 2
  • 30
  • 40