I recently read the book Getting Started with Google Guava by Bill Bejeck, published by PACKT Publishing. I have been working with Google Guava a long time and I was surprised how many new things I learned from the book, because I thought I knew this library pretty well.
The book itself is very practical and there are tons of code examples. I especially liked the fact that most code examples are written as JUnit tests, so you immediately get a feel of how to use the utility class under discussion and in which context they can be used. I want to discuss each chapter in detail now:
Basic Guava Utilities
This chapter discusses utils from Guava which you will find in many projects. The author explains the advantages of the Guava classes for String manipulations like Splitter, Joiner, Strings and Charmatcher. This chapter is pretty straightforward and if you don’t know these utilities yet, you’ll immediately see how these utils can simplify your life. Guavas Preconditions class and different builders for toString(), hashCode() and equals() methods are also discussed in this chapter.
One of the most amazing features of the Guava library are the utilities to enable functional-style programming in Java. The author discusses in-depth when to use this functional paradigm and when it’s better to stick with the imperative classic Java approach. You’ll find many examples for the use of the Function and Predicate classes of Guava. Although Suppliers are introduced in this chapter, too, I personally think the Supplier classes were not discussed extensive enough because in my opinion Guavas Suppliers are way too underrated and I use them very frequently.
The Guava originated from the google-collections library and so it’s no surprise that a big part of Guava are utils for working with Collections or completely new collection classes. The book demonstrates the use of all relevant collection utils and covers additional collection classes like Immutable Collections, Table, BiMap, etc. This chapter also introduces the Ordering class as a complement to classic Comparators.
Although very short, this is one of the chapters I enjoyed most in the book. It covers the most important classes in Guava for dealing with concurrency and introduces Monitor, ListenableFuture, FutureCallback, AsyncFunction and RateLimiter. I learned a lot here and especially Monitor and ListenableFuture are classes which I intend to use more. Especially ListenableFuture is interesting as it can completely replace classic Java Futures and are more powerful.
Guavas Cache implementation is very useful and is a lightweight alternative to libraries like EHCache. It’s widely adopted and so it’s no surprise that the Cache gets an extra chapter. For people who are looking for a lightweight, configurable and powerful local cache implementation, this chapter covers everything you will ever need to know about Guavas Cache.
I was excited to see that Guavas Eventbus is covered in a whole chapter because I use the EventBus regularly and it’s an easy and lightweight way to decouple your application. The chapter discusses how to use the event bus and and what advantage you get in your application using it. The code examples are great and show how easy it is to decouple your application components.
Finally it’s discussed how to integrate the Eventbus with your Dependency Injection Framework for convenient usage. The code examples are for spring which is a bit irritating, I personally think Google Guice would be a better fit. The concepts for integrating are the same, though.
The eight chapter discusses the shortcomings of classic Java File handling and how Guava fills this hole. It introduces many utility classes for manipulating files, file contents and folders. I personally get bored quickly when reading about file handling, but this is nothing I can blame the author for.
The last chapter covers some other utilities form Guava which are worth mentioning. It introduces Hashing functionality, Bloom Filters, and the super handy Optional class. I am using the later heavily and I think this class should have deserved more than just a very short introduction.
I was very excited from the book, it covers all relevant utils from Guava thoroughly and I learned a lot. The author decided to let code speak and included many code snippets for every topic discussed in the book. I can absolutely recommend this practical book for anyone who is not familiar with Guava (yet) and even long time users of the library will learn some new and exciting stuff.