Simplest way to iterate through a Multiset in the order of element frequency?

I just added this feature to Guava, see here for the Javadoc.

Edit: usage example of Multisets.copyHighestCountFirst() as per the original question:

Multiset<DeviceType> histogram = getDeviceStats();
for (DeviceType type : Multisets.copyHighestCountFirst(histogram).elementSet()) {
    System.out.println(type + ": " + histogram.count(type));
}

Leave a Comment