DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Google Guava: Multisets

Google Guava: Multisets

Tom Jefferys user avatar by
Tom Jefferys
·
May. 01, 12 · Java Zone · Interview
Like (0)
Save
Tweet
9.86K Views

Join the DZone community and get the full member experience.

Join For Free

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) {
        objectCounts.put(obj,0);
    } else {
        objectCounts.put(obj,count++);
    }
}
 
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);
myMultiset.add(myObject);  // add it a second time.
 
System.out.println(myMultiset.count(myObject)); // 2
 
myMultiset.remove(myObject);
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: http://docs.guava-libraries.googlecode.com/git-history/v9.0/javadoc/com/google/common/collect/Multiset.html 

 

Google Guava Google (verb)

Published at DZone with permission of Tom Jefferys, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 6 Quick Tips for Building an App
  • Privacy and the 7 Laws of Identity
  • What Is Cloud-Native Architecture?
  • 3 Pieces of Bad Advice on How to Keep Your IT Job

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo