This Guava Collections Tutorial series explains all of the main classes that make up the bulk of this powerful collections library. These collections have now been time tested and have been developed to happily coexist with the Java Collections Framework (JCF). There is another powerful collections framework called Apache Commons Collections which is very good in its own right, however, in my opinion, since Apache Commons Collections has failed to provide a generics-enabled version it isn’t as appealing as Guava.
Guava Collections Code Examples
With the following examples, we will cover many useful collections in the Guava Framework:
In this tutorial we will cover a implementing an ArrayListMultimap (which allows duplicate key/value pairs).
In this tutorial we will cover a implementing an HashMultimap (which does not allow duplicate key/value pairs).
In this tutorial we will cover a implementing an TreeMultimap (which performs Sorting according to natural order and does not allow duplicate key/value pairs).
In this tutorial we will cover a basic introduction the to Guava’s Multiset collection using easy to follow example to show you how best to implement this collection in your own code.
In this tutorial we will cover a basic introduction the to Guava’s Bidirectional Map (BiMap) using easy to follow example to show you how best to implement this collection in your own code.
In this tutorial we will show you the basics to Guava’s Table structure using easy to follow example enabling you to implement this collection easily in your own code.
In this tutorial we show you how to use Guava’s ClassToInstanceMap collection to store class type as the key in the collection and the instance of the collection as a value.
Guava Collections Framework Examples
Multimap (ArrayListMultimap Implementation)
A Multimap also called a Multihash is a variation of a Map in which multiple values or objects are associated with a single key. As the name suggests, Multimap is based on the Map interface which stores key/value pairs. It allows duplicate key/value pairs. In this example, you will notice that the value of Amaury Valdes appears in the Multimap element with a key of 777.
Multimap (HashMultimap Implementation)
A HashMultimap extends Multimap and has the added feature that it cannot hold duplicate key-value pairs. Adding a key-value pair that’s already in the multimap has no effect. In this example, the value of Amaury Valdes is not allowed for the key of 777 as an element with that key/value pair already exists. The same holds true for value David Connor with a key of 892. So in essence, by using HashMultimap behaves like a HashSet in that duplicates are not allowed.
SortedSetMultimap (TreeMultimap Implementation)
A SortedSetMultimap is a collection whose set of values for a given key are kept sorted; that is, they comprise a SortedSet. It has common features of HashMultimap as it cannot hold duplicate key-value pairs; adding a key-value pair that’s already in the multimap has no effect. However, only the values are sorted as the interface does not specify the ordering of the multimap’s keys.
A Multiset extends Multimap and has the added feature that it cannot hold duplicate key-value pairs. The Multiset is sort of a hybrid between a List and a Set — It allows duplicates but the order of the elements in the set is not guaranteed. Another name for Multiset is a Bag.
A special type of collection that ensures uniqueness of both keys and values. A BiMap ensures that all values are unique — this ensures that all values are a set. In my example, Alabama maps to the city of Montgomery. However, since we are using a BiMap we can use inverse with the value of Montgomery we can return the key associated with this value which, in this case, is Alabama.
A special collection that allows us to use two keys as indexes, a row key and a column key with a value. This structure can be viewed as something like Map<State, Map<City, Demographics>> which is a bit hard to work with. So in this example, choosing a specific state will return a Map of all the cities in that state and the Demographics object.
A special type of Map that uses the Java raw type as the key and the instance as the value. When using primitive types you will need to use the associated wrapper class for the type.