Getting Started with Google Guava

4.5 (2 reviews total)
By Bill Bejeck
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started

About this book

Java continues to maintain its popularity and is still one of the main languages used in the software industry today. But there are things in Java that are difficult to do that can be made easier; that’s where Guava comes in. Guava provides developers with a way to write better code, with less effort.

Getting Started with Google Guava will show the reader how to start improving their code from the very first chapter. Packed with examples and loads of source code, this book will have an immediate impact on how you work with Java.

This book starts with using Guava to help with the common tasks that Java developers perform. Then you’ll work your way through more specialized situations and finally some great functionality Guava provides that can add a lot of power to your applications with little effort. You will learn about Guava’s famous Collections classes that add unique features, like the Bi-Map, to Java’s already great Collection classes. We’ll see how to add some functional programming aspects to our code. We will also learn about using a self-loading cache for improved performance in our applications, and how to use the EventBus to create software that takes advantage of event-based programming.

Publication date:
August 2013


Chapter 1. Getting Started

In this chapter, we are going to cover a little bit on Guava's history. Then, we are going to make a case on why you should use a well-established library instead of "rolling your own". We are going to talk about where you can get the Guava library and how to install it and finally, how to set up the source code that comes with this book.


Introducing Google Guava

What is Google Guava? Starting out originally in 2007 as the "Google Collections Library", which provided utilities for working with Java collections, the Google Guava project has evolved into being an essential toolkit for developers working in Java. There is something for everyone in Guava. There are classes for working with strings, collections, concurrency, I/O, and reflection. The Function interface gives us the ability to incorporate functional programming into Java and greatly simplify our code. The Supplier interface helps with creational patterns. But Guava is more than just abstractions that take some of the boilerplate out of Java, or convenience methods that we all feel should have been in Java to begin with. It's about writing a good code and making it more resilient and concise. So my suggestion is to not just use Guava, but look at the source code and get a feel of how things are done. Then try to apply the same principles you've learned to your own code. Finally, have fun!


The case for using Guava

As software developers, we like to think we can do it all. We instinctively want to write our own libraries for handling things we see on a day-to-day basis. Of course, we think the code we've written is bullet proof, and we know why we've written unit tests, and they all pass! Well, I have some bad news for you, we all are not as smart as we'd like to be. Actually, it's really not about how smart you are. It's more about writing code that's not only unit tested, but is also being used by a large group of developers and having their input weigh in on the code. Guava is used by hundreds of production applications, and as of July 2012, there were a staggering 286,000 individual unit tests in the guava-tests package. So when it comes down to it, you are far better off using a library such as Guava, than rolling your own. Besides, according to Larry Wall (the author of Perl), one of the best qualities of a software engineer is laziness, not in the "I don't want to work" way but in the "Why reinvent the wheel when this works so well" way. Really good developers will look for an established library to help with a problem before starting to write their own.


What is this book about?

Our goal for the book is that it will always sit next to your computer while you are coding. When you come across a situation where you need to know how to use something from Guava, or what Guava has that could solve your problem, our hope is that this book will have the answer, and if not, at least point you in the right direction. This book will have source code for every topic covered. Most of the time, the source code will be in the form of unit tests. Sometimes, coming up with meaningful examples can be difficult, and a unit test will quickly show how the code is supposed to work. Also, having unit tests will be invaluable as Guava tends to have a frequent release schedule, and running the tests will give you a quick indication if anything has changed from the previous release. While it will be impossible to cover every part of the Guava library, we've tried to make the book comprehensive and cover most of what we think a typical developer will find useful. Finally, we hope that the book will be as easy to read and enjoyable as it is useful.


Installing Guava

To start working with Guava, all you need to have is Java 1.6 or a higher version installed. The version of Guava covered in this book is 14, which is the latest as of this writing. The following are the steps you need to perform to get started with Guava:

  1. Guava can be downloaded directly by navigating to and clicking on the guava-14.jar link.

  2. If you are working with GWT and would like to take advantage of Guava in your code, there is also a GWT compatible version that can be downloaded by clicking on the guava-gwt-14.jar link on the same page. A separate version for GWT is required because everything in the standard Guava distribution will not be compiled to JavaScript by the GWT compiler.

  3. Once the JAR file is downloaded, add it as an external library to your IDE (IntelliJ, NetBeans, or Eclipse). If you are working with a text editor (Sublime Text 2 or TextMate), add the JAR file to your classpath.

  4. The API docs for Guava can be found at

You are now ready to start working with Guava.


Using Guava with Maven or Gradle

It's possible to use Guava with build tools such as Maven or Gradle.

To use Guava in your Maven projects, add the following to the dependencies section of your pom.xml file:


If you are using Gradle, first add the Maven Central Repository (if you haven't already) by adding the following to your build.gradle file:

  repositories {

Then, add the following highlighted section to the dependencies section of the build.gradle file:

 dependencies {
       compile group: '' name: 'guava' version: '14.0'

For more information on Maven, go to, and for more information on Gradle, go to

It's important to mention that Guava has only one dependency, JSR-305.


JSR-305 is a specification for defining annotations that can be used by tools for detecting defects in Java programs. More information is available at

If you are not planning on using the JSR-305 JAR directly, you don't need to include it with your dependencies. But if you are going to use JSR-305, you will need to explicitly define that dependency, as it is not going to be pulled in automatically. Also, if you plan to use Guava from within Scala, you will have to include the JSR-305 JAR file. While the Java compiler does not require the library containing the annotations when compiling, the Scala compiler currently does. While this may change in the future, for now, if you want to use Guava with Scala, you will need to have the JSR-305 JAR file in your classpath as well.

Getting the source code for the book

The source code for the book is structured as a Java project, with a structure consistent with that of either a Gradle or Maven project. As mentioned earlier, most of the source code will be in the form of unit tests. If you don't have either Gradle or Maven installed, I strongly recommend that you install one of them, as it makes running the unit tests easy and will pull down Guava and all the dependencies for the project.

The following are the steps for obtaining and working with the source code from the book:

  1. Download the source code from

  2. Extract the zipped source file to a location on your computer.

  3. Change the directory to guava-book-code directory.

  4. If you have Gradle installed, run gradle install.

  5. If you have Maven installed, run mvn install.

After following these steps, you will have Guava installed as well as the dependencies needed for the source code from the book. If all went well, you should have seen a bunch of unit tests being executed and they should have all passed. I strongly recommend using either of the build tools previously mentioned, with the source code. This will make it very easy to change the versions of Guava as it evolves and runs the tests for the book's source code and see if anything has changed. If you don't have either of the build tools installed, you will need to download the following dependencies to run all the examples listed in the book:

The source code for the book was written on a MacBook Pro v10.7.5, using Java 7, the Gradle build system, and the IntelliJ IDE.


Downloading the example code

You can download the example code files for all Packt books that you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.



So far we've gone over a brief history of Guava, and how it can improve the quality of your code as well as make your job a little easier, if not more fun. We also saw the importance of using well-tested and widely used libraries instead of rolling your own. Finally, we went over where to get Guava from, how to install it, and how to get the source code for the book. In the next chapter, we begin our exploration of Google Guava by covering the basic utility classes found in the package along with the ComparisonChain class from the the package.

About the Author

  • Bill Bejeck

    Bill Bejeck is a senior software engineer with 10 years experience across a wide range of projects. Currently he is working on the storage and analysis of financial data using Hadoop. He has a B.A in Economics from the University of Maryland and an M.S in Information Systems from Johns Hopkins University. Bill also enjoys blogging at

    Browse publications by this author

Latest Reviews

(2 reviews total)
Excellent book and experience in web site is perfect
Although the name says it from the start ("getting started with..") I would have preferred the book to have a more deep coverage of the guava framework. Beside, its a great book to start, plenty of pratical examples
Book Title
Access this book and the full library for FREE
Access now