If you replace your HashMap<U, Set<V>>
by a HashMultimap<U, V>
(they're equivalent, and the Multimap
is easier to use), you can now use Multimaps.invertFrom()
which will populate a Multimap<V, U>
.
Note that as the Javadoc mentions, if you use an ImmutableMultimap
, you can then directly call ImmutableMultimap.inverse()
.