RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Comparing the Google Collections Library with the Apache Commons Collections

Like Apache's Jakarta Commons Collections, the recently released Google Collections Library also enhances the Java Collections Framework but offers some unique enhancements you may prefer.

hen I first learned about the Google Collections Library, I was puzzled. The Google Collections Library enhances the JDK's Java Collections Framework, and its developers plan to make it part of JDK 7. As a user of Apache's Jakarta Commons Collections, I didn't understand why Google created an API to enhance the Java Collections Framework when Apache had already done so. I read in an interview with one of the Google Collections Library creators that a motivation for creating a new library was to provide JDK 5 Generics support, which Jakarta Commons Collections lacks. I also read that Jakarta Commons Collections contains a few classes that violate the Java Collections Framework specification.

Upon investigating these claims I found that Jakarta Commons Collections indeed doesn't support JDK 5 Generics—a definite flaw. However, I found this SourceForge project, which claims to be a JDK 5 Generics-enabled version of Jakarta Commons Collections.

As for Java Collections Framework violations, I found that only the Bag interface methods in Jakarta Commons Collections violate the spec (as per the JavaDoc), and the Bag interface by its nature warrants this violation. Take the add method in the Bag interface as an example. The Java Collections Framework spec stipulates that the add method must always return true, but Bag does not do this when an object of the same type as one already in Bag is added. In that case, it only increases the count and the method returns false.

Coincidently, I discovered the src folder of the Jakarta Commons Collections project had not been updated in two months.

So I decided to explore the Google Collections Library further and find out what it had to offer a Java developer like me who's been using Jakarta Commons Collections. In this article, I describe the packages and classes in the Google Collections Library (hereafter referred to simply as the Library). I then compare and contrast these classes with those in the Apache Jakarta Commons Collections (hereafter referred to simply as Commons Collections).

Google Collections Library Packages
The Library is organized into two packages:

  • com.google.common.base – This package contains a few common utility classes, which can be used even without the Java Collections Framework.
  • com.google.common.collect – This package hosts all the enhancements to the Java Collections Framework.

This organization enables a programmer to grasp the Library quicker than he or she would learn Commons Collections. For instance, whereas decorator classes are scattered throughout all the packages in Commons Collections, the Library keeps them all in one package (com.google.common.collect) and prefixes them with 'Forwarding<decorator type>'.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date