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
- New types
- Static utilitis
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:
- An immutable, hash-based Map
- An immutable SortedMap
- An immutable BiMap
- An immutable collection.
- An immutable hash-based multiset
- A high-performance immutable Set
- 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.
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.
- A multiset that supports concurrent modifications
- Multiset implementation backed by an EnumMap.
- A multiset which forwards all its method calls to another multiset. (Decorator pattern alike)
- Multiset implementation backed by a HashMap.
- Immputable hash-based.
- A Multiset implementation with predictable iteration order.
- Multiset which maintains the ordering of its elements.
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
- A multimap which forwards all its method calls to another multimap.
- Multimap that uses an ArrayList to store the values for a given key
- Immutable ListMultimap
- Implementation of ListMultimap that supports deterministic iteration order for both keys and values.
- The multimap does not store duplicate key-value pairs.
- Immutable SetMultimap
- Multimap that does not allow duplicate key-value entries and that returns collections whose iterators follow the ordering in which the data was added to the multimap.
- Multimap whose keys and values are ordered by their natural ordering or by supplied comparators.
BiMap is the map with unique values. Than it can be inverted so bimap.inverse().inverse() == bimap. There are few implementation of BiMap interface:
- BiMap backed by two EnumMap instances
- BiMap backed by an EnumMap instance for keys-to-values, and a HashMap instance for values-to-keys.
- BiMap backed by two HashMap instances.
- 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.
- Provides filter with Predicate and transform with Function methods
- Function extension eg compose
- 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.
- Provides such methods as new* for easier Map creation, difference method, filter* methods with Predicate, fromProperties, transformValues with Function parameter.
- Provides methods such as new*, synchronized*, unmodifiable*
- Provides contact and newArray methods
- Provides methods: differece, filter, intersecton, new*, union
- Some Supplier extensions
- Predicate extension eg. and, or, not etc
- Static methods to be called at beginning of methods to check preconditions.
- Helper for Object class has great hashCode(Object …) method for generating hash code for multiple objects.
And more …
- 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.
- ConcurrentMap builder, providing any combination of these features: soft or weak keys, soft or weak values, timed expiration, and on-demand computation of values.
- A transformation from one object to another. Used in transform static utility methods for Collection API extension.
- Determines a true or false value for a given input. Used in filter static utility methods.
- Class that supply object of single type. Used in new* static utility methods
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!
- What is the Google Collections Library? – interview with authors Kevin Bourrillion and Jared Levy.
- Google Collections Home Page