I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Reasons to Use Google Collections

01.05.2010
| 19976 views |
  • submit to reddit

After seeing that Google Collections has just had it's final 1.0 release at the end of last year, I thought that I should take a look to see what the advantages are to using this library rather than the default collections provided in the JDK. Google Collections is available as a zip file containing a jar file, source code and javadoc. 

The Java Collections API is there to be extended, and Google Collections does this very well. Straight away, one of the best things that I see about the library is the inclusion of a MultiMap type.  This allows you to associate multiple values with one key in your map. Seeing this feature made me realise how much wasteful code I've written in the past.

To illustrate this, let's use a phone book example, where each person could have 1 or more phone numbers. Previously, I would have stored the phone numbers in a seperate list per person as follows:

Map<Person, List<PhoneNumber>> phoneBook = new HashMap<Person, List<PhoneNumber>>();

With MultiMap, this is much neater: 

Multimap <Person, PhoneNumber> newPhoneBook = ArrayListMultimap.create();

And with multimap there's no need for managing the lists of phone numbers per person.  When I have a new addition it's just a matter of: 

Person me = new Person("James");
newPhoneBook.put(me, new PhoneNumber(111));
newPhoneBook.put(me, new PhoneNumber(201));

instead of the old verbose way of managing the ArrayList: 

Person me = new Person("James");

if(phoneBook.get(me) == null)
{
phoneBook.put(me, new ArrayList<PhoneNumber>());
}
phoneBook.get(me).add(new PhoneNumber(111));

While on the topic of managing the data that you're processing, the Preconditions allow you to run checks on your parameters that you are passing around.

Preconditions.checkNotNull(me);

I know it's available with assertions in Java, but this way you will always have your checks enabled. You can also easily check, and throw an IllegalArgumentException for a particular argument, by evaluating an expression: 

Preconditions.checkArgument((i > 0), "The value i has to be greater than zero");

There are a number of other useful features such as the ability to create immutable collections quickly: 

ImmutableList<Integer> numbers = ImmutableList.of(1, 2, 3, 5, 8, 13, 21, 34);

Previously you would have needed to create the list as normal, and then use Collections.unmodifiableList(). 

To get a really good overview of the library, check out this video from a 2008 talk by Kevin Bourrillion:

 

In summary, this library gives you a nicer way of dealing with collections, and makes your code much more readable and to the point. 

Tags:

Comments

Mikael Grev replied on Tue, 2010/01/05 - 12:02pm

Google collections is absolutely fantastic.

However, a 578k compressed jar. That's a lot to carry for improved collection classes. I guess it doesn't matter much on a server but a bit heavy if deploying client software. Both regarding download and startup time.

Not sure how to solve these things though. Maybe some sort of standard way to denote that your library is obfuscatable, possibly trough standard annotations, so that unused parts can be safely removed at build time. This means no reflection into those parts though, and carefully handling serialization.

Anyways, it's a great library and the size problem is more of a Java problem in general.

Maybe I'm just a bit cranky today... ;)

Cheers,
Mikael

Andrew Thorburn replied on Tue, 2010/01/05 - 11:25pm

I don't think the ~580KB is a problem. A lot of popular libraries are significantly bigger than that, and could realistically be included in desktop applications (such as Jasper Reports or SWT). If it is a problem, I suppose you could compile it yourself, stripping out debug symbols, variable names and the like, but that would make it a major pain to debug if something went wrong... And I've got no idea whether that would reduce the file size or not... Apart from that it sounds pretty awesome, and like something I should have a look into. Thanks!

Boaz Lev replied on Wed, 2010/01/06 - 8:34pm

First, it is Multimap, and not MultiMap. Second, notice that the license is Apache 2, which is an LGPL family, so it does not make your production an open source only because it uses google-collections. But Apache 2 does mean that if you ever claim lawsuit against Google for any reason - your license to use google-collections automatically revoked. This way Google protects itself from lawsuits coming from other big companies. Other than that - great package... Boaz Lev

Tomek Kaczanowski replied on Thu, 2010/01/07 - 4:35am

when I see something like this showing up in my code:

Map<Person, List<PhoneNumber>> phoneBook = new HashMap<Person, List<PhoneNumber>>(); 

then I know, that I'm doing it wrong - I should create my own PhoneBook type here, and don't use collections at all.

What do you think ? 

James Sugrue replied on Thu, 2010/01/07 - 5:50am in response to: Tomek Kaczanowski

Well, you're probably right there. But for this code example I wanted to illustrate it with something that's easy to imagine.

Oliver Weiler replied on Thu, 2010/01/07 - 3:07pm

I just love the static factory methods, a feature I reeeally miss in the Java Collections Framework...

Victor Tsoukanov replied on Fri, 2010/01/22 - 7:55am

when you are using
Preconditions.checkArgument((i > 0), "The value i has to be greater than zero");
you create new string value that may not be used at all. Imaging how many such strings you can introduce and how long will GC collect them. And look at the following
if (i > 0) {
  throw new IllegalArgumentException ("The value i has to be greater than zero");
}
agreed, some of them will be cached, may be in enterprise java it does not matter, but when you develop high-loaded application (e.g. in Telco domain) it matters

Jo Binder replied on Thu, 2010/11/25 - 6:48am

Thanks for explaining the concept of google collections in detail.Your video was informative as well.Keep it up !!!

 <a href="www.pitneyworks.co.uk">Document binders</a>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.