Google Guava is a very useful library for Java developers. The library contains a lot of utility classes and methods for working with Strings, Collections, files and much more. Code written with Google Guava is often much shorter and better readable and maintainable than the Java equivalent. The documentation provided by Google Guava is very basic and doesn’t have clear example to show the usage immediately.
The book “Getting Started with Google Guava - Write better, more efficient Java, and have fun doing so!” written by Bill Bejeck is a very practical book to learn about the Google Guava library. The book has 126 pages. Bill uses a lot of sample code in the book to explain the different parts of the Google Guava library. The book is also a good reference book. Although in practice you’ll probably also want to browse the API documentation to learn about all classes and methods that are available.
In the first chapter of the book we learn how we can download Google Guava and how we can configure Maven and Gradle (!) to have Google Guava as a dependency in our Java projects. In the next chapter we immediately start with useful utility classes for working with Strings, which is of course very common in everyday programming. I really like the fact that a lot of sample code is used to show the usage of Google Guava. We learn how we can join and split collections and String values with the Joiner and Splitter classes. Also we see how we can manipulate String values with the CharMatcher class. The author shows how we can add defensive checks in our code with the Preconditions class. And finally some basic utility classes and methods for easier writing of toString(), hashCode() and compareTo() methods for Java objects are shown.
Chapter 3 is about the Function and Predicate classes in the Google Guava library. Using these classes we can use a more functional style of programming. This results in better readable code and often also better maintainable code. Bill explains clearly how we can use Function and Predicate classes. Also the usage of the Functions and Predicates utility classes is explained with clear examples. Finally the Supplier and Suppliers classes are covered.
The following chapter is about working with collections. As developers we write a lot of code where collections are involved, we work with lists, sets and maps often. Google Guava contains a lot of useful classes and methods that make working with collections easier. Not all methods are covered, because that is not doable, but some very useful and important methods are covered with example. Also the new collection type Table that is added by Google Guava is explained. Finally ranges, immutable collections and sorting of collections are described.
Google Guava adds some extra features to the standard Java concurrency. Especially when working with asynchronous events and using the Future class. The concurrency support is the subject of chapter 5.
In the next chapter the author covers the cache implementation of Google Guava. The implementation is more powerful than a simple map, but less powerful than libraries like EHCache. All classes and methods we can use for caching are explained and small code examples show how we can use the cache.
Google Guava also gives us an EventBus class we can use in publish/subscribe mechanisms in our Java code. Via the EventBus object can communicate and share information. Bill explains how we create an EventBus class and an asynchronous version AsyncEventBus. Also through clear code examples we learn how to publish event to the event bus implementation and how to add listeners for events (publish/subscribe).
Another area in programming we stumble upon in our daily work is working with files. In the next chapter we see utility classes and methods found in Guave to work with files. We see how we read and write information easily, without much code.
In the last chapter Bill explains some other useful tools found in Google Guava, like the Hashing claas, BloomFilter, the Optional class and finally the Throwables class.
Getting Started with Google Guava is clearly written and has a lot of example code, which I always find nice to see, because it shows immediately how to apply Google Guava. The book is very practical and is also a good reference. The Google Guava tools contain even more classes and methods than can be covered in this book, so it is still good to look at the API documentation to learn more features of Guava. But if you’ve never used Guava before, then this book will give you a good taste and you probably want to include it in your Java projects from now on.