Guava – Google Collections

Guava

Guava is Google Core Libraries for Java 1.5, it include extension libraries which provides many common pieces of code. Long long time ago apache starts with commons-* libraries, but unfortunately most of them lack java 5 support.

Currently Google Collection is included into Guava and on 30 Dec google released Google Collection 1.0-fianal version. This version is not 1.0 but it has frozen API. It’s nearly done, but it is used in google production projects now.

So, what’s inside ?

  • Immutable Collections
  • Multisets
  • Multimaps
  • New types
  • Static utilitis

Immutable Collections

JDK currently has utility wrapper in Collections which can change our map/set/list into unmodifiable, but this is just a partial solution because internally our collection is mutable. Immutable means that we can never change the object state. So there are:

com.google.common.collect.ImmutableMap
An immutable, hash-based Map
com.google.common.collect.ImmutableSortedMap
An immutable SortedMap
com.google.common.collect.ImmutableBiMap
An immutable BiMap
com.google.common.collect.ImmutableCollection
An immutable collection.
com.google.common.collect.ImmutableMultiset
An immutable hash-based multiset
com.google.common.collect.ImmutableSet
A high-performance immutable Set
com.google.common.collect.ImmutableSortedSet
An immutable SortedSet that stores its elements in a sorted array.

All this Collections don’t allow Null values and Maps don’t allow Null keys or values.

Multisets

Multiset is a type of collection which allows duplicate elements and has unordered equality (lists have ordered equality). A multiset is also called bag. Multiset is better from list in such operation as contains(), checking if two collection are equal ignoring order and providing histograms (providing info about element count). Additionally Multiset performance depends on the number of distinct elements.

There are a lot of different implementation of Multiset interface.

com.google.common.collect.ConcurrentHashMultiset
A multiset that supports concurrent modifications
com.google.common.collect.EnumMultiset
Multiset implementation backed by an EnumMap.
com.google.common.collect.ForwardingMultiset
A multiset which forwards all its method calls to another multiset. (Decorator pattern alike)
com.google.common.collect.HashMultiset
Multiset implementation backed by a HashMap.
com.google.common.collect.ImmutableMultiset
Immputable hash-based.
com.google.common.collect.LinkedHashMultiset
A Multiset implementation with predictable iteration order.
com.google.common.collect.TreeMultiset
Multiset which maintains the ordering of its elements.

Multimaps

Similar to Multiset is Multimap iterface. It provides abstraction like a Map but keys aren’t unique or we can thing about this that unique key has a list of values, so if you have somewhere in the code type such as Map> Multimap is for you. As 1.0 version we’ve got Multimap interface implementation such as:

BiMap

BiMap is the map with unique values. Than it can be inverted so bimap.inverse().inverse() == bimap. There are few implementation of BiMap interface:

com.google.common.collect.EnumBiMap
BiMap backed by two EnumMap instances
com.google.common.collect.EnumHashBiMap
BiMap backed by an EnumMap instance for keys-to-values, and a HashMap instance for values-to-keys.
com.google.common.collect.HashBiMap
BiMap backed by two HashMap instances.
com.google.common.collect.ImmutableBiMap
Immutable BiMap

Static Utility Class

In this category there are a lot of class which has only static members to extend and provide function which are useful but not available today.

Collections2
Provides filter with Predicate and transform with Function methods
Functions
Function extension eg compose
Lists
Provides such methods as new* for easier List creation and two additional methods partition(create sublists of the same size) and transform which takes Function as a parameter and apply it to every list element.
Maps
Provides such methods as new* for easier Map creation, difference method, filter* methods with Predicate, fromProperties, transformValues with Function parameter.
Multimaps
Provides methods such as new*, synchronized*, unmodifiable*
ObjectArrays
Provides contact and newArray methods
Sets
Provides methods: differece, filter, intersecton, new*, union
Suppliers
Some Supplier extensions
Predicates
Predicate extension eg. and, or, not etc
Preconditions
Static methods to be called at beginning of methods to check preconditions.
Objects
Helper for Object class has great hashCode(Object …) method for generating hash code for multiple objects.

And more …

Ordering
Comparator with added methods to support common functions such as min, max etc. So instead of iterate over list to find min value, we can use min(Iterable) method.
MapMaker
ConcurrentMap builder, providing any combination of these features: soft or weak keys, soft or weak values, timed expiration, and on-demand computation of values.
Function
A transformation from one object to another. Used in transform static utility methods for Collection API extension.
Predicate
Determines a true or false value for a given input. Used in filter static utility methods.
Supplier
Class that supply object of single type. Used in new* static utility methods

Final thoughts

So…, in my opinion this library is must have in every project and there are a chance that this library will be standard JDK7 library, there are a lot of structures and static utility methods which makes our code much cleaner and simpler. Googler made great job!

References