Find top N elements in a Multiset from Google Collections?

Posted by dfrankow on Stack Overflow See other posts from Stack Overflow or by dfrankow
Published on 2010-06-12T15:25:22Z Indexed on 2010/06/12 15:32 UTC
Read the original article Hit count: 234

Filed under:
|
|

A Google Collections Multiset is a set of elements each of which has a count (i.e. may be present multiple times).

I can't tell you how many times I want to do the following

  1. Make a histogram (exactly Multiset)
  2. Get the top N values from the histogram

Examples: top 10 URLs, top 10 tags, ...

What is the canonical way to do #2 given a Multiset?

Here is a blog post about it, but that code is not quite what I want. First, it returns everything, not just top N. Second, it copies (is it possible to avoid a copy?). Third, I usually want a deterministic sort, i.e. tiebreak if counts are equal.

© Stack Overflow or respective owner

Related posts about java

Related posts about guava