I apologize if this is a duplicate, but I couldn't find any answer that specifically answered this particular issue.
I have a HashMap that contains a string key paired with a Set value. I want to sort the values in my map based on the length of the set. Consider:
HashMap<String, Set<String>> myMap;
Contains:
{"A", {"Dukmerriot", "King", "Pumpkin"}}
{"B", {"Steve"}}
{"C", {"Jib", "Jab", "John", "Julie"}}
{"D", {"Apple", "Amy", "Unicorn", "Charlie", "Raptor"}}
{"E", {}}
I want to be able to get the list {"D", "C", "A", "B", E"}
(which designates the order of the sets from largest to smallest) from myMap
efficiently.
Is there a way to sort a collection of sets based on their length other than creating a wrapper class that implements Set and overriding the compareTo
method?
EDIT: I should specify that I don't NEED to use a HashMap to maintain this collection. I could use a TreeMap or something, but I'm not sure if that's possible since Set doesn't implement Comparable.