Capturing Set Behavior with Mutating Elements

Posted by Carl on Stack Overflow See other posts from Stack Overflow or by Carl
Published on 2010-04-12T20:31:35Z Indexed on 2010/04/12 20:32 UTC
Read the original article Hit count: 358

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about set