views:

44

answers:

1

It seems like there should be a removalAllOccuring(Collection) (or similiar) method in Multiset. A sort of analog to remove(Object,int) and removeAll(Collection). Short of that method, what is the best way to accomplish its intent. I wrote a small JUnit driver to demonstrate:

/**
 * @see http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multiset.html
 * @see http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/HashMultiset.html  
 */
@Test
public void testBag() {
    Multiset<String> bag1 = HashMultiset.create();
    bag1.add("a", 2);
    bag1.add("b", 3);
    bag1.add("c", 3);

    logger.debug(bag1);
    Multiset<String> bag1Copy = HashMultiset.create(bag1);

    Multiset<String> bag2 = ImmutableMultiset.of("a","b","b","b","c","c");
    Multiset<String> expected = ImmutableMultiset.of("a","c");

    for( String el :  bag2.elementSet() ) {
        bag1.remove( el, bag2.count(el));
    } // end for

    Assert.assertEquals(expected, bag1);

    bag1Copy.removeAll(bag2);
    logger.debug( bag1Copy );
}

The output:

[b x 3, c x 3, a x 2]
[]

I'm thinking there might be a a way provided that I'm missing, or a different/better way than looping over the collection to be removed. Also note, that I could just as easily have a use case where the collection to be removed is a List

+2  A: 

There is nothing that will do this yet. :( There is an internal (to Google) feature request about this and other similar methods (as static methods for the Multisets class). I'll try to get it pasted into an externally-visible feature request that you could keep an eye on.

EDIT: I hadn't been planning to accept other kinds of Collections besides a Multiset as the parameter to such methods, though. Will have to think about that.

Kevin Bourrillion