Java developers frequently complain that many common programming tasks in Java require a copious amount of code. While some seek concision in the land of alternate, more succinct JVM languages, others aim to stretch Java's existing language features to support API-level solutions to verbosity.
Two recent examples of the API-based approach are Google's Collections and Guava libraries. In a recent blog post, Beautiful Code with Google Collections, Guava and Static Imports Aleksander Stensby illustrates how these libraries can make Java programming a more rewarding experience:
Guava is still a rather immature library that is subject to changes over the coming months... Google Collections will become part of Guava once Collections reaches it’s 1.0 release. Guava (and Google Collections) have been pioneered by several Google developers and is widely used in their “myriad of Java projects...”
You can benefit from using Guava with Collections to reduce the amount of boilerplate code in your projects (and gain access to the new and faster data structures that they provide!).
As an example, Stensby shows how to create collection instances with Google Collections:
Thanks to generics and those handy factory methods that the Collections guys have provided us with, we no longer have to write things that Java really should understand, right? ... I know this will be part of JDK 7, and that’s great. But Google Collections is here, now...
Similar to the static utility methods that com.google.common.collect.Maps provide, we also have Lists and Sets...
Lists.newArrayList();
Sets.newHashSet();
Stensby also demonstrates how to initialize a collection with some values:
Apart from the handy and clean ways of creating collections and populating them, we are also provided with lots of additional utility methods like filtering, set intersections and union, ordering and even some neat functional stuff!
In the second part of his blog post, Stensby turns to the Guava library:
Guava provides us with a comprehensive extension to the Core Java Libraries. Among other things we gain access to utilities for working with primitives through the Ints, Doubles, Floats, Shorts, Bytes and Bools classes in the com.google.common.primitives package. The com.google.common.io package provides utilities for working with streams, buffers, files etc, and the concurrent package provides classes like Futures, Callables and Executors to ease the pain of writing concurrent code. In addition to all of this, Guava also provides us with some additions to Collections and a really neat CharMatcher class, Joiner and Splitter classes.
What do you think of Google's Collections and Guava libraries as tools to help reduce boilerplate Java code?
So, I like this. I use static functions and static imports for cleanup quite a bit. I used to write abstract classes with protected-static-final functions and protected constructors just to remove one extra name+dot when dereferencing function calls, and this is much cleaner.
I also like naming these function-bundles (they're not really classes, sort of) Thing+s instead of ThingUtils.
These are pretty much our only mechanisms for "extending" primitives / final classes / the jdk lang/util/io packages, huh? No one has come up with any other solutions to this?