Guava/Google Collections – Unleash the power

I have been using Google’s core Java Library Guava (which includes Google Collections) extensively for the past few months in my project and man it’s an amazing and elegant framework to code with. Once you start using it, you can rarely get off it for its power and nimbleness.

The biggest use of it is reduce the Lines of code and avoid unnecessary looping through Java Collection Objects be it for filtering or searching or sorting. And what more it gives ability to do some amount of functional programming like in Java by passing around Predicates and Functions.

Some of basic classes which comes with Guava are

Splitter – which can split a String a produce a Iterable out of it. If you use Java String split you end up getting an array of String objects. But getting an Iterable out of it is really cool thing, especially if you get some request parameters which are comma separated you want to extract the individual parameter values.

String pattern = “Foo,Bar,Boo”;
Iterable pattLst = Splitter.on(“,”).split(pattern);

The above is a naive example, you have option to use an regex pattern, ignore empty strings etc.

Complimenting the Splitter is a Joiner, which joins the elements of a Collection for you, by joining with a pattern you provide.
Suppose you have list of values in a List which you want to concatenate with “,” and insert as a String to the db, Joiner will do the job for you in a single line rather than writing 5-10 lines in the conventional approach.

Iterable nameLst = new ArrayList();
String names = Joiner.on(“,”).join(nameLst); //voila!

The real power comes with Collection APIs. There are different components in it – Google Collection provides entirely new data structures like MultiMap, BiMap etc. Also it provides powerful ways to filter, search, sort and manipulate any Java Collection Object.

Let us explore the Predicate Interface, which facilitate in filtering of any Collection.

Suppose you have a class Student.

class Student {
String name;
int marks;

Further you have a list of students in a List

ArrayList studentLst = new ArrayList();

Out of the student list if you have to filter the Students with marks greater than 80, you can create a Predicate for it.
We can write a function which creates the predicate and it can used anywhere.

So in class Student we can put a static function.

public static Predicate getStudentMarkPredicate(final int mark)
Predicate studentPredicate = new Predicate() {
public boolean apply(Student input) {
if ( mark >= input.getMark() )
return true;
return false;
return studentPredicate;

The above function creates a Predicate, the predicate is defined such that it will return true for any input Student if his mark is greater than or equal to the given Mark.

So now if you want to filter the studentLst to get the Students who have scored above 80, you can do it with this small elegant code below

Iterable studentsAbvEighty = Iterables.filter(studentLst, Student.getStudentMarkPredicate(80));

Well you can pass any arbitrary mark above a get the list of students matching them.

Providing even more gist is the Predicates class which has some predefined Predicates like and, not, or, in etc. These are real power tools for you search and filter multiple lists elegantly.

Suppose you have two Lists
List studentLst; //master list
List failedStudents;//failed students

Now if you need to filter the passed students from studentLst you can do it with a single line of code like this
Iterable passedStudents = Iterable.filter(studentLst, and(in(studentLst),not(in(failedStudents))));

Look how readable the above code and no clunky mechanism of looping. The above is a very simplified example, but you can imagine the power by chaining ands/ors and nots and ins to produce a complex filter.

Another powerful class is the Ordering which can be used for Comparator Chaining a produce a multi-column sort. I would explore that in the next post.

So the key take away is if you are doing Java Development and haven’t used Google Collections, your moment has arrived for it now. Explore and have fun!


One thought on “Guava/Google Collections – Unleash the power

  1. Pingback: Functional programming the LISP way | on the count of three

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s