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
- Make a histogram (exactly Multiset)
- Get the top N elements by count from the histogram
Examples: top 10 URLs (by # times mentioned), top 10 tags (by # times applied), …
What is the canonical way to do #2 given a Google Collections 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. Other nits: it’s not static, etc.
I wrote methods with the basic functionality you’re asking for, except that they perform copies and lack deterministic tie-breaking logic. They’re currently internal to Google, but we may open-source them at some point. This Guava issue has the method signatures.
Their algorithm is similar to the blog post: sorting a list of entries. It would be faster, but more complicated, to use a better selection algorithm.
EDIT: since Guava 11, this is implemented