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 BiMap is the map with unique values. Than it can be inverted so bimap.inverse().inverse() == bimap. There are few implementation of BiMap interface:
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.
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! Tags: java
BiMap
Static Utility Class
And more …
Final thoughts
References