I'm trying to have a sorted map Map<Integer,Set<Integer>>
which keeps elements sorted based on the size()
of the value set.
In practice this is a map of a node to the other nodes connected to that node. I want to quickly (O(logn)) access the node with the most edges without having to sort every time.
For example the order should be:
3 => {1,2,4,5}
12 => {1,2,3}
14 => {3,2,3}
65 => {3,8}
6 => {2}
2 => {5}
Since TreeMap
won't do it since I can't sort based on values, I probably need to roll something custom.
EDIT: The size of the Set may indeed change which may over-complicate things even more
What would be the simplest way to achieve this?