Thursday, 22 September 2011


Guava part 1 - MultiMaps
Guava part 2 - BiMaps

Continuing this tour of Guava we get to the Multiset. I probably don't use this as much as Multimaps or Bimaps, but it certainly does have it's uses.

So what's a Multiset then?

Well as you might be able to guess it's a set that can hold multiple instances of the same object.

Isn't that just a List?

In Java there are two basic differences between Lists and Sets. Lists can hold duplicates of the same object, and Lists are always ordered. Sets can't hold duplicates, and there's no guarantee of order by the Set interface. (Some implementations - LinkedHashSet, SortedSet etc. - do of course provide a guaranteed order!)

So a Multiset occupies a sort of grey area between a List and a Set. Duplicates allowed, but no guaranteed order.

This collection is also sometimes called a Bag, in fact this is what Apache Commons Collections calls it's Mutlisets.

So what would I use one for?

The great thing about Multisets is they keep track of the counts of each particular object in the set. So you can use them for counting stuff.

Have you ever written code like the following:
Map<MyClass,Integer> objectCounts = new HashMap<MyClass,Integer>();

public void incrementCount(MyClass obj) {
    Integer count = objectCounts.get(obj);
    if (count == null) {
    } else {

public int getCount(MyClass obj) {
    Integer count = objectCounts.get(obj);
    if (count == null) {
        return 0;
    } else {
        return count;
Bit unwieldy? Lets see how we might use a Multiset instead:
Multiset<MyClass> myMultiset = HashMultiset.create();

MyClass myObject = new MyClass();

myMultiset.add(myObject);  // add it a second time.

System.out.println(myMultiset.count(myObject)); // 2

System.out.println(myMultiset.count(myObject)); // 1

As you can see that's much simpler! It's even possible to add/remove more than one object at at time
Multiset<MyClass> myMultiset = HashMultiset.create();

MyClass myObject = new MyClass();
myMultiset.add(myObject,5); // Add 5 copies of myObject

System.out.println(myMultiset.count(myObject)); // 5

myMultiset.remove(myObject,2); // remove 2 copies

System.out.println(myMultiset.count(myObject)); // 3
Pretty useful eh? As usual there's several implementations available depending on your requirements, and I recommend taking a look at the API:


immo abroad said...

The best Real estate agent in Spain at our immoabroad site in Spain. Find and contact the most renowned agent in Spain and the best professionals agent.

Charles Sorensen said...

The quantity of times a component has a place with the multiset is the variety of that part. The aggregate number of components in a multiset, including rehashed participations, is the cardinality of the multiset. For instance, in the multiset {a, a, b, b, b, c} the multiplicities of the individuals a, b, and c are separately 2, 3, and 1, and the cardinality of the multiset is 6. To recognize sets and multisets, a documentation that consolidates square sections is once in a while utilized: the multiset {2, 2, 3} can be spoken to as [2, 2, 3].[2] In multisets, as in sets and as opposed to tuples, the request of components is unimportant: The multisets {a, a, b} and {a, b, a} are equivalent.
best essay writing service

Anita Romanenko said...

I really like your modern stylish ideas which you have shared about manipulative. This blog provide extraordinary and exact information for which I was searching.

Paulina Cameron said...

I must thank the author for this brilliant blog. I have never seen someone being this good at programming. Keep up the good work! I reckon I had some problems with my computer’s programming and I failed to order an essay from the NYC Resume Services but now I know that having taken your advice I will never have such issues again.

Neil Jakson said...

In mathematics, a multiset (or bag) is a generalization of the concept of a set that, unlike a set, allows multiple instances of the multiset's elements. For example, {a, a, b} and {a, b} are different multisets although they are the same set.
cheap assignment Writing Services

Alex said...

Great post.Duplex roller chain liked this blog.Keep it up

jeo john said...

Multi sets are explained very well.drum roller equipment interested in this post.Hope for more in future

Writer said...

Sometimes, it's more important to practice on programming tasks than write assignments. That's why students buy essays online.

mermaid said...

Thank you so much for your post.Its really awesome post.Thank you so much for your effort.list of courses in uk . Thank You

dipali sharma said...

It is apparent that regardless of how appealing your blog posts are, on your ID, people will never ever include their sort on these events such as images and video clips without solid 'call for action'. buy targeted facebook likes