Question

I would like to know a few practical use-cases (if they are not related/tied to any programming language it will be better).I can associate Sets, Lists and Maps to practical use cases.

For example if you wanted a glossary of a book where terms that you want are listed alphabetically and a location/page number is the value, you would use the collection TreeMap(OrderedMap which is a Map)

Somehow, I can't associate MultiSets with any "practical" usecase. Does someone know of any uses?

http://en.wikipedia.org/wiki/Multiset does not tell me enough :)

PS: If you guys think this should be community-wiki'ed it is okay. The only reason I did not do it was "There is a clear objective way to answer this question".

Was it helpful?

Solution

Lots of applications. For example, imagine a shopping cart. That can contain more than one instance of an item - i.e. 2 cpu's, 3 graphics boards, etc. So it is a Multi-set. One simple implementation is to also keep track of the number of items of each - i.e. keep around the info 2 cpu's, 3 graphics boards, etc.

I'm sure you can think of lots of other applications.

OTHER TIPS

A multiset is useful in many situations in which you'd otherwise have a Map. Here are three examples.

Suppose you have a class Foo with an accessor getType(), and you want to know, for a collection of Foo instances, how many have each type.

Similarly, a system could perform various actions, and you could use a Multiset to keep track of how many times each action occurred.

Finally, to determine whether two collections contain the same elements, ignoring order but paying attention to how often instances are repeated, simply call

HashMultiset.create(collection1).equals(HashMultiset.create(collection2))

In some fields of Math, a set is treated as a multiset for all purposes. For example, in Linear Algebra, a set of vectors is teated as a multiset when testing for linear dependancy. Thus, implementations of these fields should benefit from the usage of multisets.

You may say linear algebra isn't practical, but that is a whole different debate...

A Shopping Cart is a MultiSet. You can put several instances of the same item in a Shopping Cart when you want to buy more than one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top