Book Image

Getting Started with Google Guava

By : Bill Bejeck
Book Image

Getting Started with Google Guava

By: Bill Bejeck

Overview of this book

<p>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.</p> <p>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.</p> <p>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.</p>
Table of Contents (16 chapters)
Getting Started with Google Guava
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Multimaps


While maps are great data structures that are used constantly in programming, there are times when programmers need to associate more than one value with a given key. While we are free to create our own implementations of maps that have a list or set as a value, Guava makes it much easier. The static factory methods return map instances that give us the familiar semantics of the put(key,value) operation. The details of checking if a collection exists for the given key and creating one if necessary, then adding the value to that collection, are taken care of for us. Let's dive in and explore this powerful abstraction.

ArrayListMultimap

ArrayListMulitmap is a map that uses ArrayList to store the values for the given key. To create an ArrayListMultimap instance, we use one of the following methods:

  • ArrayListMultimap<String,String> multiMap = ArrayListMultimap.create();

  • ArrayListMutilmap<String,String> multiMap = ArrayListMultimap.create(numExcpectedKeys,numExpectedValuesPerKey...