MultiSet

Why have a multiset? A multiset is a collection that supports order-independent equality, like Set, but may have duplicate elements. A multiset is also sometimes called a bag.

In JavaTools a multiset can simply be created with JMultiSet[]. It then has to be filled with elements afterwards.

In[62]:=

Out[62]=

However, it's also possible to create a new multiset and supply initial values during creation:

In[63]:=

Out[63]=

JMultiSetSize[] returns the number of elements in the multiset, and JMultiSetIsEmpty[] shows if the multiset is empty:

In[64]:=

Out[64]=

In[65]:=

Out[65]=

A new element is added to the multiset with JMultiSetAdd[]:

In[66]:=

Out[66]=

The whole list of elements in the multiset can be shown with JSetToArray[]:

In[67]:=

Out[67]=

The type of elements that can be put in a multiset can be anything: Integer, Real, String, or even List:

In[68]:=

Out[68]=

In[69]:=

Out[69]=

In[70]:=

Out[70]=

In[71]:=

Out[71]=

In[72]:=

Out[72]=

In[73]:=

Out[73]=

Note that with a multiset it IS possible to add an element that is already in the multiset. That is the crucial difference between a set and a multiset: There can be no duplicates in the set!

In[74]:=

Out[74]=

In[75]:=

Out[75]=

JMultiSetAdd[] also can be used to add a specified number of occurrences of an element to the multiset It retunrs the count of the element BEFORE the operation, possibly 0:

In[76]:=

Out[76]=

In[77]:=

Out[77]=

In[78]:=

Out[78]=

In[79]:=

Out[79]=

With MultiSetAddAll[] we can add a whole list of elements to the multiset element-wise:

In[80]:=

Out[80]=

In[81]:=

Out[81]=

In[82]:=

Out[82]=

In[83]:=

Out[83]=

JMultiSetCount[] returns the number of occurences of that element in the multiset ("multiplicity").

In[84]:=

Out[84]=

In[85]:=

Out[85]=

In[86]:=

Out[86]=

In[87]:=

Out[87]=

JMultiSetElementSet[] returns the set of DISTINCT elements in the multiset (unlike JMultiSetToArray[], which returns all elements of the multiset):

In[88]:=

Out[88]=

With JSetRemove[]we can remove an element that is in the multiset:

In[89]:=

Out[89]=

In[90]:=

Out[90]=

With JSetRemove[]we can also remove a (sub)set of elements that occur in the multiset a specified number of times. JMultiSetRemove[] returns the number of occurrences BEFORE removal (which could possibly be zero).

In[91]:=

Out[91]=

In[92]:=

Out[92]=

In[93]:=

Out[93]=

In[94]:=

Out[94]=

With JMultiSetContains[] we can see if the multiset contains a particular element:

In[95]:=

Out[95]=

In[96]:=

Out[96]=

In[97]:=

Out[97]=

In[98]:=

Out[98]=

Note that JMultiSetAddAll[]adds all elements in the list to the multiset individually, whereas JSetAdd[] adds all elements in the list to the multiset as one list element:

In[99]:=

Out[99]=

In[100]:=

Out[100]=

In[101]:=

Out[101]=

JMultiSetSetCount[]lets you set the count (number of occurrences for a given element in the multiset. JMultiSetSetCount[]will add or remove as many elements as is necessary to attain the desired count. It returns the count of the element before the operation:

In[102]:=

Out[102]=

In[103]:=

Out[103]=

In[104]:=

Out[104]=

JMultiSetClear[] completely clears the entire multiset, returning an empty multiset and resetting its size to 0.

In[105]:=

In[106]:=

Out[106]=

In[107]:=

Out[107]=

In[108]:=

Out[108]=