Is there a non-duplicate "list" implementation with order by frequency?
For instance :
TreeSet<String> cities = new TreeSet<String>();
cities.add("NYC"); // Ordered list is [NYC]
cities.add("Boston"); // Ordered list is [Boston, NYC] (alphabetical order)
cities.add("NYC"); // Ordered list is [NYC, Boston] because NYC was added twice
cities.add("Philly");
cities.add("Philly");
cities.add("Philly"); // Ordered list is now [Philly, NYC, Boston]
This is tricky with the basic JDK, and not doable with a pure
Set
, but if third-party libraries are fair game, you might use Guava'sMultiset
. The methodMultisets.copyHighestCountFirst
sorts a given multiset by the number of occurrences of each element.