Showing posts with label google. Show all posts
Showing posts with label google. Show all posts

Wednesday, 13 April 2011

Introduction to Google Collections

We all know what is Java Collection Framework? Also, we have seen collection framework changing over past few years. We have seen Java introducing new APIs, and also generics getting introduced in the Tiger release. Now we have Google Collections, which are based on Java collection framework but enhance it to introduce new most commonly required features. These features are required during any real life use of Java Collection Framework. Multi key collections, collection having combined features of two Java Collections, and so many APIs that allow many day to day transformations of these collection objects constitute Google Collections.

Below are the important elements of Google Collections.

Collection Types OR Interfaces: These define the types of collections available. These interfaces define contract of manipulation of these types independent of their implementation. Most of the collection types are to serve a specific purpose.

Classes: These abstract or concrete classes provide partial or complete implementation of collection types. They construct enhanced data structures which can be directly used for a defined purpose.

Annotations: Three annotations constitute this group. These annotations tag a class as Google Web Toolkit compatible, Google Web Toolkit incompatible and Visible to Test.

Base OR Util: These are a set of interfaces and types that are used to support the main collection and annotation classes.

Google Collection Types

Google Collections provide additional collection types to offer additional but common features. These advanced types are derived from Java Collection types, hence they continue to offer the features of underlying Java type. In addition to that there are many additional features encapsulated in these new types, so that we can directly use them instead of re-inventing wheel. Let us see these interfaces and features of those interfaces.

Multimap: This interface is based on Map interface. Map stores key-value pairs. Map interface does not allow duplicates. Multimap also stores key-value pairs, just that it allows duplicate keys. This interface is also Google Web Toolkit compatible. In addition to the java.util.Map methods, the additional methods are – containsEntry() which checks if a key-value pair exists in MultiMap, keys() returning a MultiSet of keys, and few other methods.

SetMultiMap: This combines features of a Set and a MultiMap. This interface extends from MultiMap. Though the key-value pairs of SetMultiMap can have duplicate keys, but it cannot contain duplicate values. This provides duplicate key feature of MultiMap and adheres to the contract of Set by containing only unique key values. This means the values inserted against a key, forming a collection, will not be duplicate.

ListMultiMap: This is another extension of MultiMap. This allows duplicate key-value pairs. The value part of a pair fulfills List contract. List contract includes maintaining order of (collection) values inserted against the key and random access of these values.

SortedSetMultiMap: This extends from SetMultiMap. Collection entered as a value against a key is sorted.

MultiSet: Alternate name of this collection is bag. This is has origin from set but differs in basic constraint of set, which is – in mathematics a set cannot have duplicate values. Multiset can have. If the values are equal in Multiset then you can get ‘count’ of those values.

BitMap: It can also be called as invertible Map. It guaranties uniqueness of both keys and values. Hence it can be inverted to convert values into keys.

ClassToInstanceMap: This allows type safe value addition to the map. Raw Java types e.g. Collection can be added to the Map.