Capturing Set Behavior with Mutating Elements
- by Carl
Using the Guava library, I have the following situation:
SetMultimap<ImmutableFoo, Set<Foo>> setMM = HashMultimap.create();
Set<Foo> mask = Sets.newHashSet();
// ... some iteration construct
{
setMM.put(ImmutableFoo1, Sets.difference(SomeSetFoo1,mask));
setMM.put(ImmutableFoo1, Sets.difference(SomeSetFoo2,mask));
mask.add(someFoo);
}
that is, the same iteration to create the setMM is also used to create the mask - this can of course result in changes to hashCode()s and create duplicates within the SetMultimap backing.
Ideally, I'd like the duplicates to drop without me having to make it happen, and avoid repeating the iteration to separately construct the multimap and mask. Any easy libraries/Set implementations to make that happen? Alternatively, can you identify a better way to drop the duplicates than:
for (ImmutableFoo f : setMM.keySet()) setMM.putAll(f,setMM.removeAll(f));
revisiting the elements is probably not a performance problem, since I could combine a separate filter operation that needs to visit all the elements anyway.