Did you ever felt that working with the Java Collections Framework could be more elegant or efficient? Then you really should consider to use the Google Collections API. It's a great utility library every Java developer should know. Take the time to read this introduction to easily getting started with Google Collections.
The Google Collections Library 1.0 is a set of new collection types, implementations and related goodness for Java 5 and higher, brought to you by Google. It is a natural extension of the Java Collections Framework you already know and love.
Working with Lists
First let me show you some convenient List utilities. The class Lists contains plenty of static utility methods for building and manipulating lists (analog Sets and Maps for set and map utilities). Let's take a look at the following example source code:
The code is self-explanatory. I'm using some factory method to create an array list. Then this list will be transformed to another list by applying some generic function to all lists elements. The transformed list will be printed to the console by using a Joiner which let you easily build strings from collections. The result looks like this:
Extensions to Iterators and Iterables
Similiar to Lists, Sets and Maps Google Collections serves convient utilities for iterating over collections of elements. The classes Iterators and Iterables contain various helpful static methods for manipulating, combining, filtering or transforming iterable collections.
To cut a long story short check out this code snippet:
First a list will be constructed containing some strings and a null value. Then this list will be filtered, we only want all the strings starting with B and the null value. Finally the result will be printed to the console replacing all null values with B000. Executing the code results in:
Building Predicate Logic
Google Collections makes it easy to work with logical predicates. The class Predicates contains appropriate static methods such as and, or, not or in to build complex predicates.
As you can see in the following example these predicates are clearly represented in combination with static imports (a Java 5 feature). It's also possible to combine predicates with functions as you can see in the second example.
Combining and Modifying Comparators
One thing I really like about Google Collections is the class Ordering which let you easily combine multiple Comparators to perform flexible comparisons on runtime. Think of a class Person with different members such as first and last name. We want to be able to order persons by multiple members without implementing verbose comparisons. This can be easily achived with Google Collections.
First we define two simple Comparators for each member involved by the ordering. Then we can build different orderings with ease using the comparators in combination with static methods from the class Ordering.
As you can see its easy to combine the comparators to complex orderings. Besides you don't have to bother with null values. Executing the code sample results in:
Working with Maps
Google Collections comprises very nice Map support. Not only does the library provide convient utility methods via the class Maps. Also it serves own map implementations like BiMap which preserves uniqueness not only of its keys but also of its values.
This example shows the functionality of Bimaps. Putting equal keys or values results in overriding the appropriate entries. The result looks like this:
Google Collections enables you to easily build immutable maps via builder:
As you can see computing the difference between two maps is quite comfortable using the utility class Maps. Here is the result of this snippet:
Also it's easy to filter a map by some predicate:
The result looks like this:
Finally, let's apply some transformations to the values of a map:
This was a short introduction to Google Collection. This article demonstrated only a minor subset of what the API contains. Feel free to explore the rest of the API by yourself.