This is a long overdue post after my Guava on Android post from February. Since then I’ve been using Guava in pretty much every Java project I was involved in and I still find new stuff that makes my code both shorter and clearer. Some random examples:
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Lists, Maps and Sets classes contain bunch of newFooCollection, which effectively replace the diamond operator from JDK7, but also allow you to initialize the collection from varargs.
Sets also contain the difference, intersection, etc. methods for common operations on sets, which a) have sane names, unlike some stuff from JDK’s Collections, and b) doesn’t change operands, so you don’t have to make a defensive copy if you want to use the same set in two operations.
Speaking of defensive copying: Guava has a set of Immutable collections, which were created just for this purpose. There are few other very useful collections:
LoadingCache, which you can think of as a lazy map with specified generator for new items;
Multiset, very handy if you need to build something like a histogram;
Table if you need to lookup value using two keys.
The other stuff I use very often are
Preconditions. It’s just a syntactic sugar for some sanity checks in your code, but it makes them more obvious, especially when you skim through some unfamiliar code. Bonus points: if you don’t use the return values from
checkPositionIndex, you can remove those checks from performance critical sections using Proguard.
On Android you have the
Log.getStackTraceString() helper method, but in plain Java you’d have to build one from
Throwable.getStackTrace(). Only you don’t have to do this, since Guava have
Throwables.getStackTraceAsString() utility method.
Guava introduces also some functional idioms in form of
Collections2.filter, but I have mixed feelings about them. On one hand sometimes they are life savers, but usually they make the code much uglier than the good ol’ imperative for loop, so ues them with caution. They get especially ugly when you need to chain multiple transformations and filters, but for this case the Guava provides the
None of the APIs listed above is absolutely necessary, but seriously, you want to use Guava (but sometimes not the latest version). Each part of it raises the abstraction level of your code a tiny bit, improving it one line at the time.