MultiMap

Why have a multimap? A multimap is a collection similar to a Map, but which may associate multiple values with a single key. Typical applications include sales departments where one salesperson has several customer accounts, but for every customer account there is only one salesperson. Another typical example is a telephone number database. A person may have several telephone numbers, but a given telephone number is owned only by one person.

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

"multimap_1.gif"

"multimap_2.gif"

JMultimapSize[] returns the number of elements in the Multimap, and JMultimapIsEmpty[] shows if the Multimap is empty:

"multimap_3.gif"

"multimap_4.gif"

"multimap_5.gif"

"multimap_6.gif"

"multimap_7.gif"

"multimap_8.gif"

"multimap_9.gif"

"multimap_10.gif"

With JMultimapKeys[] and JMultimapValues[] we can look at the entries in the Multimap:

"multimap_11.gif"

"multimap_12.gif"

"multimap_13.gif"

"multimap_14.gif"

as {key, value} pairs (=entries):

"multimap_15.gif"

1 1
5 77
123 {2,4,6,8}
key value

The crucial difference between a Multimap and a Map is that a Multimap kan have multiple values for the same key:

"multimap_16.gif"

"multimap_17.gif"

"multimap_18.gif"

"multimap_19.gif"

"multimap_20.gif"

1 1
5 85
5 97
5 77
123 {2,4,6,8}
key value

However, the SAME {key, value} pair can occur only once:

"multimap_21.gif"

"multimap_22.gif"

"multimap_23.gif"

1 1
5 85
5 97
5 77
123 {2,4,6,8}
key value

JMultimapSize[] displays the size of the Multimap, that is the number of entries ({key,value} pairs):

"multimap_24.gif"

"multimap_25.gif"

JMultimapIsEmpty[] shows if the Multimap is empty:

"multimap_26.gif"

"multimap_27.gif"

JMultimapContainsKey shows if the Multimap contains the specified key:

"multimap_28.gif"

"multimap_29.gif"

JMultimapContainsValue[] shows if the Multimap contains the specified value:

"multimap_30.gif"

"multimap_31.gif"

JMultimapGet[] obtains ALL values for the specified key and returns them as a list of values for that key, preserving the order in which multiple values for a key were entered:

"multimap_32.gif"

"multimap_33.gif"

"multimap_34.gif"

"multimap_35.gif"

"multimap_36.gif"

"multimap_37.gif"

"multimap_38.gif"

"multimap_39.gif"

"multimap_40.gif"

"multimap_41.gif"

JMultimapRemove[] removes the entry consisting of the specified {key,value} pair.

"multimap_42.gif"

"multimap_43.gif"

"multimap_44.gif"

1 1
5 85
5 97
123 {2,4,6,8}
key value

JMultimapRemoveAll[] removes ALL entries for the specified key:

"multimap_45.gif"

"multimap_46.gif"

"multimap_47.gif"

1 1
123 {2,4,6,8}
key value

JMultimapClear[] empties the Multimap completely and sets the size to 0:

"multimap_48.gif"

"multimap_49.gif"

"multimap_50.gif"

"multimap_51.gif"

"multimap_52.gif"

Spikey Created with Wolfram Mathematica 7.0