TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Impossible to remove an element from a multiset! #75

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, once an element has been added to a Multiset, there is no way to remove
it. The "removeAllOcurrences" set its count to 0. But there is no actual
way to get rid of it completely. 

I tried to remove the Multiset.Entry itself (from the the Multiset's
entrySet), but this didn't seem to update the Multiset, and iterating over
the Multiset still had the element-- just its count is automtically set to 0!

Thanks, A 

Original issue reported on code.google.com by angus.fo...@gmail.com on 8 Jun 2008 at 8:56

GoogleCodeExporter commented 9 years ago
Ok-- if I have

Multiset.Entry entry = ... some Multiset.Entry

then this does work:

myMultiset.entrySet().remove(entry);

Perhaps there should be some sort of convenience method in the Multiset 
interface for
removing an element completely, as well as for decrementing some or all of its 
count.

Original comment by angus.fo...@gmail.com on 8 Jun 2008 at 9:04

GoogleCodeExporter commented 9 years ago
Are you sure? Which multiset implementation are you using? Could you provide a
failing test case?

The AbstractMapBasedMultiset.removeAllOccurences code, copied below, removes the
entry from the backing map.

  private int removeAllOccurrences(@Nullable Object element,
      Map<E, AtomicInteger> map) {
    AtomicInteger frequency = map.remove(element);
    if (frequency == null) {
      return 0;
    }
    int numberRemoved = frequency.getAndSet(0);
    size -= numberRemoved;    
    return numberRemoved;
  }

Original comment by jared.l....@gmail.com on 8 Jun 2008 at 4:17

GoogleCodeExporter commented 9 years ago
I'm closing this issue, since I couldn't reproduce it. If you can provide some 
code
that behaves incorrectly, please reopen this.

Original comment by jared.l....@gmail.com on 10 Jun 2008 at 7:00

GoogleCodeExporter commented 9 years ago
This is ironic, because the method that you want, removeAllOccurrences(), both 
(a)
exists and (b) is being removed!!

So it's the exact opposite of what you'd have wanted.  However,
removeAllOccurrences(a) is identical in behavior to both elementSet().remove(a) 
and
the (soon-to-be-added) setCount(a, 0), so it became highly redundant.

Incidentally, there is zero difference between setting an element's count to 
zero and
removing the element completely.  They are the same thing.

Original comment by kevin...@gmail.com on 10 Jun 2008 at 7:06