Sunday, January 31, 2010

Exploring google collections - Part 1

Google Collection 1.0 released
Google collections finally hit version 1.0. API provides rich data structures building on JDKs collection API. It also provides utility classes for working with collections. With this post I want to focus on few google collections classes and demonstrate how they help in working with collections. In part 1 of the post, we will deal with very simple example. Later in part 2 of the post, we will work with more involved real life example.

We will focus on these classes:
Scenarios
While working with collections you may have noticed that code gets cluttered with for loops, nested for loops, or with for loops with embedded if loops. In most of these scenario what you are trying to do is to iterate through a given collection, either to search for specific elements or to operate on specific elements.

Lets break down the typical cases:
1. Iterating through a given collection and finding a specific element(s) based on criteria

for(Element element: elementsCollection){
     if(element.equals(something){ //Or any other test criteria based on state of element object
          //either add to collection of elements
          //or return this element
     }
}
2. Iterating though a given collection and if certain criteria matches perform operation on the collection

for(Element element: elementCollection){
     if(element.equals(something){
          //or operate on this element
     }
}
As an outcome of this, code ends up with
  • Code duplication of your iteration logic
  • Code duplication of your condition (matching criteria)
  • Code duplication of operation on the specific elements (This however can be avoided, if operation is re-factored into a method)
Google Collections Approach
Lets start with very simple example, here is how google collection can help:
1. Define you matching criteria using Predicate like

            Predicate<String> validEmailAddress = new Predicate<String>(){
                public boolean apply(String emailAddress){
                    return emailAddress.contains("@");
                }
            };
This can be used as

List<String> emailList = Lists.newArrayList("abc@gmail.com", "xyz@gmail.com", "aaagmail.com");
Collection<String> validEmailList = Collections2.filter(emailList, validEmailAddress);

2. Define your operation on element using Function like

        Function<String, String> extractUserName = new Function<String, String>(){
            public String apply(String emailAddress)
            {
                String userName = emailAddress.substring(0, emailAddress.indexOf('@'));
                return userName;
            }
        };
This can be used as

        List<String> emailList = Lists.newArrayList("abc@gmail.com", "xyz@gmail.com", "aaa@gmail.com");
        List<String> userNameList = Lists.transform(emailList,extractUserName);

We will take on more complex DOM-like tree based data structure in part 2 of the post...

Blogged with the Flock Browser
Post a Comment