If I have a Guava Multimap, how would I sort the entries based on the number of values for the given key?
For instance:
Multimap<String, String> multiMap = ArrayListMultimap.create();
multiMap.put("foo", "1");
multiMap.put("bar", "2");
multiMap.put("bar", "3");
multiMap.put("bar", "99");
Given this, when iterating over multiMap, how would I get the “bar” entries to come first (since “bar” has 3 values vs. only 1 for “foo”)?
Extract the entries in a list, then sort the list :
Then iterate over the entries.
Edit :
If what you want is in fact iterate over the entries of the inner map (
Entry<String, Collection<String>>), then do the following :