The Kaptain on … stuff

15 May, 2010

Achieving Groovy-like Fluency in Java with Google Collections

Posted by: TheKaptain In: Development

One of the most compelling things about using Groovy is the fluent and concise syntax, as well as the productivity and readability gains that come out of it. But there’s no reason not to take advantage of some of the same techniques and some library support, in this case google-collections, to make Java code easy to write AND to read.

Creating Collections

Groovy really shines for this one, making the creation of Lists and Maps, empty or populated, an absolute breeze. Java has some help for creating basic Lists but begins to struggle when creating maps. This is an area that google-collections can help in, especially in regards to creating immutable Maps.

[groovy language=”true”]
//Empty Lists
List<String> groovyList = []
List<String> javaList = new ArrayList<String>()
List<String> googleList = Lists.newArrayList() //can omit generics

//Populated Lists
List<String> groovyList = ["1", "2"]
List<String> javaList = Arrays.asList("1", "2")
List<String> googleList = Lists.newArrayList("1", "2")

//Immutable Lists
List<String> groovyList = ["1", "2"].asImmutable()
List<String> javaList = Collections.unmodifiableList(Arrays.asList("1", "2"))
List<String> googleList = ImmutableList.of("1", "2")

//Empty Maps
Map<String, String> groovyMap = [:]
Map<String, String> javaMap = new LinkedHashMap<String,String>()
Map<String, String> googleMap = Maps.newLinkedHashMap()

//Immutable Maps
Map<String, String> groovyMap = ["a":"1", "b":"2"].asImmutable()

Map<String, String> javaMap = new LinkedHashMap<String,String>()
javaMap.put("a", "1")
javaMap.put("b", "2")
javaMap = Collections.unmodifiableMap(javaMap)

//OR(works only in Java, will not compile in Groovy)
Map<String, String> javaMap = new LinkedHashMap<String, String>()
{
{
put("a", "1");
put("b", "2");
}
};

Map<String, String> googleMap = ImmutableMap.of("a", "1", "b", "2") //clunky syntax but it works
[/groovy]

Filtering Collections

Groovy provides the very handy ‘findAll’ method that allows you to filter a Collection by applying a Closure. Google-collections provides similar facilities using the Predicate interface and two filter methods available statically on Collections2 and Iterables. This would also be a lot more readable if the Predicate definition were extracted but I wanted to show that it’s still possible to create them in-line quickly.

[groovy language=”true”]
import static com.google.common.collect.Collections2.*

List<Integer> toFilter = [1, 2, 3, 4, 5]
List<Integer> groovyVersion = toFilter.findAll{ it < 3}
List<Integer> googleVersion = filter(toFilter, new Predicate<Integer>()
{
public boolean apply(Integer input)
{
return input < 3;
}
};)
[/groovy]

Joining Collections into a String Representation

This one has come up pretty often over the years, and it’s not surprising that where the JDK hasn’t provided, enterprising developers have added support through libraries. The problem is: given a Collection of objects, create a String representation of that Collection suitable for view from a consumer of the system. And admit it – the first time you hand-coded the algorithm you left a trailing comma, didn’t you? I know I did.
Groovy has fantastic support for this use-case by simply including the ‘join’ method on Lists. Google-collections utilizes static calls on the Joiner class along with a simple DSL-like syntax to achieve the same effect. Throw in a static import to make it even more concise and it does a fine job. These two examples yield the same result.

[groovy language=”true”]
import static com.google.common.base.Joiner.*
def toJoin = [‘a’, ‘b’, ‘c’]
def separator = ‘, ‘

//groovy version
def groovyJoin = toJoin.join(separator)

//google-collections version
def googleJoin = on(separator).join(toJoin)

[/groovy]

And google-collections also supports join for Maps, something missing from Groovy(although not very hard to implement).

[groovy language=”true”]
import static com.google.common.base.Joiner.*
def toJoin = [1: ‘a’, 2: ‘b’, 3: ‘c’]
def separator = ‘, ‘
def keyValueSeparator = ‘:’

//results in ‘1:a, 2:b, 3:c’ which is essentially what is returned from Groovy map.toMapString()
def googleVersion = on(separator).withKeyValueSeparator(keyValueSeparator).join(map)

//results in ‘1 is a and 2 is b and 3 is c’
googleVersion = on(" and ").withKeyValueSeparator(" is ").join(map)

//doing the same in Groovy is slightly more involved, but really not that bad
def groovyVersion = toJoin.inject([]) {builder, entry ->
builder << "${entry.key} is ${entry.value}"
builder
}.join(‘ and ‘)
[/groovy]

Multimaps

Multimaps are one of the more interesting parts of google-collections, at least to me. Have you ever found yourself writing code to create a Map of keys to Lists? Well the various Multimap implementations in google-collections mean you never have to write that boilerplate kind of code again. Here’s a “first-stab” effort to simulate a fairly generic Multimap with pure Java code.
[groovy]
public class JavaMultimap
{
private Map<Object, List<Object>> multimap = new LinkedHashMap<Object, List<Object>>();

public boolean put(Object key, Object value)
{
List<object> objects = multimap.get(key);
objects = objects != null ? objects : new ArrayList<object>();
objects.add(value);
multimap.put(key, objects);
return true;
}
}
[/groovy]

And the same thing in Groovy, achieving a slightly smaller version.

[groovy]
class GroovyMultimap
{
Map map = [:]

public boolean put(Object key, Object value)
{
List list = map.get(key, [])
list.add(value)
map."$key" = list
}
}
[/groovy]

I did some primitive timings comparing Java, Groovy and google-collections Multimaps implementations and, as you’d pretty much expect, google clearly takes the lead. Where things really start to get interesting though is when you start using the Multimap in Groovy code. Imagine if you will that you want to iterate over a collection of Objects and map some of the properties to a List. Here’s a contrived example of what I’m talking about, but applying this same pattern to domain objects in your application or even a directory full of xml files is pretty much the same. If you look closely you’ll notice that Groovy actually makes this a one liner to extract all values for a property across a List of Objects(used in the assertion), but I imagine that Multimap is probably a better alternative for large data sets.

[groovy]
class GoogleCollectionsMultiMapTest
{
private Random random = new Random()

@Test
public void testMultimap()
{
def list = []
10.times {
list << createObject()
}
List properties = [‘value1’, ‘value2’, ‘value3’]
Multimap multimap = list.inject(LinkedListMultimap.create()) {Multimap map, object ->
properties.each {
map.put(it, object."$it")
}
map
}
properties.each {
assertEquals (multimap.get(it), list."$it")
}
}

Object createObject()
{
Expando expando = new Expando()
expando.value1 = random.nextInt(10) + 1
expando.value2 = random.nextInt(100) + 100
expando.value3 = random.nextInt(50) + 50
return expando
}
}
[/groovy]

So Where Does This Get Us?

Between google-collections and the newer guava-libraries that contain it, there’s lots of help available for simplifying programming problems and making your code more readable. I haven’t even touched on the newly available support for primitives, Files, Streams and more, but if you’re interested in reducing the amount of code you write AND simultaneously making it more readable you should probably take a look. There’s a very nice overview available in part one and part two by Aleksander Stensby. And here’s a closer look at what google-collections can do for you.

Source Code

As per usual, source code is available at github as a maven project. Big thanks to the Spock team for sharing how they configure GMaven to properly utilize Groovy 1.7. Please feel free to take a look and comment here. Thanks!

10 Responses to "Achieving Groovy-like Fluency in Java with Google Collections"

1 | Tweets that mention Achieving Groovy-like Fluency in Java with Google Collections | The Kaptain on ... stuff -- Topsy.com

May 15th, 2010 at 8:52 pm

Avatar

[…] This post was mentioned on Twitter by kellyrob99, groovyblogs.org. groovyblogs.org said: Achieving Groovy-like Fluency in Java with Google Collections — http://bit.ly/akdoxM — The Kaptain on … stuff […]

2 | Kartik Shah

May 16th, 2010 at 6:52 am

Avatar

Very nice post on google collection usage.

Thanks for posting.

If you are interested, check out my blog post on google collection here:

http://blog.kartikshah.info/2010/01/exploring-google-collections-part-1.html

http://blog.kartikshah.info/2010/02/exploring-google-collections-part-2_05.html

3 | TheKaptain

May 16th, 2010 at 8:05 am

Avatar

Thanks for linking Kartik, your articles are a very nice dive into Predicates and Functions and all the fun you can have with them. Nice job, especially on the second one!

4 | Blog bookmarks 05/17/2010 « My Diigo bookmarks

May 16th, 2010 at 7:30 pm

Avatar

[…] Achieving Groovy-like Fluency in Java with Google Collections | The Kaptain on … stuff […]

5 | And bod

May 16th, 2010 at 10:29 pm

Avatar

Yes, the Google collections are a nice tool, but I fail to see how Groovy is superior in this aspect. I mean Most of the simplified Groovy synthax (at least in this cases) comes from the lack of options that it has over Java. I mean there’s a reason why you do new LinkedList() sometimes and new Vector() other times, it’s not just “useless verbooseness that Groovy <> avoids”

6 | TheKaptain

May 17th, 2010 at 5:45 am

Avatar

Hopefully we can at least agree on the brevity of the Groovy language – you type less to achieve the same result as in Java( http://bit.ly/ceEmTZ or http://groovy.codehaus.org/Differences+from+Java ) .
I’m not entirely sure what you mean by “the lack of options that it(Groovy) has over Java”, since with very few edge cases you can compile almost any Java syntax with the Groovy compiler…
As to the reason why you would choose a particular data structure, that really has much more to do with your actual use case than the verbosity of the language, doesn’t it?
Regardless, the point of the article was not to prove that Groovy is “superior”, but that good ideas for expressing business logic in a terse yet readable syntax are not limited to any particular language or library. And that in order to write that sort of code as a Java developer there are alternatives available to you at the extremely low cost of reading some APIs, allowing you to concentrate on the IDEAS of what you’re trying to accomplish instead of on the boilerplate.

Anyhow, thank you very much for the comment and hopefully you’ll be back soon!

7 | Achieving Groovy-like Fluency in Java with Google Collections … | java

May 17th, 2010 at 5:16 pm

Avatar

[…] URL: Achieving Groovy-like Fluency in Java with Google Collections … and-concise, and-readability, and-the, come-out, compelling-things, fluent, groovy, productivity, […]

8 | Manfred Moser

May 17th, 2010 at 8:02 pm

Avatar

Nice summary post. I was waiting for that hint towards Guava (got that) and the potential that Google Collection makes its way towards standard Java.

Oh and btw. Guava works on Android too 😉

9 | TheKaptain

May 17th, 2010 at 8:42 pm

Avatar

Nice! But then again, of course it does 😉

Comment Form