For experienced Java Engineers, Guava Library is a well-known set of core support libraries open-sourced by Google (on Github since recently), but very often it’s associated to an extension set of Java Collections Framework.
So, let’s dig into with the aim to better understanding its real benefits.
Technically speaking, the Project breaks it down as follow.
- Basic Utilities. Preconditions, facilities to work with null easier, Ordering as well as Throwable utilities.
- Collections. A powerful extension of Java Collections Framework that implements very common used Data Structures, like Multi Maps and Sets, Bidirectional Maps, etc.
- Caches. Read- and Write-through cache implementations and many other facilities to make easy the development of local caching.
- Concurrency. Facilities like ListeneableFuture (an extension of Java Future with callback capabilities) and Service (abstracting start and stop behaviors) make a hard problem like concurrency far easier.
- Event-driven Components interaction (namely, EventBus). Taking benefit of Pub/Sub interaction model to implement communication among different modules.
- I/O Facilities. Handling efficiently both byte and char streams.
- String Processing. Joining, Splitting, Merging, Padding String made easy.
- Functional Idioms. An attempt to simplify the pieces of code that naturally might be expressed by the means of functional idioms.
- Hashing. Bringing in your module/class a sophisticated way to make use of hashing.
- Ranges. Working with ranges on sets of Comparable types.
- Primitives. Wappers around primitive types (i.e. short, int, long, char, etc.) to make use of general-purpose utilities in the context of collections of objects.
- Reflection. Overcoming some limitations of the type-erasure in case of generics: querying and manipulating generic types at run-time.
- Math. A comprehensive implementation of high performance math facilities, not natively provided by the JDK.
Definitely, a very rich set of supports that needs time to be acquainted to. Personally, I used extensively Guava’s Basic Utilities, Collections and Caches with an incredible boost in my projects: by making use of so well thought, coded and tested facilities, many lines of codes are saved. Collections are really powerful, data structures like Multi Maps, Multi Sets, Bidirectional Maps (i.e. forward and backward maps to link values to keys and vice versa) are built-in features, and they can be instantiated in many different ways: for example, immutable instances of these collections can be obtained directly by the framework with no further work. Caches are another very important support provided by Guava. How many times in your developer life did you face the problem to implement an effective local caching layer? Well, with Guava this problem can be easily solved with few lines of code. Out-of-the-box, a very powerful implementation of the Cache concept is provided; features like eviction strategies, cache loading, cache building, etc. are fully implemented and tested.
Concurrency, EventBus and I/O facilities are features that Middlware/Infrastructure Engineers use daily in their activities. They ‘re a comprehensive catalog of APIs that makes possible to develop efficient services with very few lines of code by following the framework, and so in a clean way.
String processing, Ranges, Primitives and Math are common utilities too, that in terms of utilization frequency may be compared to previously said Collections.
Latest, but not last in terms of importance, Reflection APIs. They provide a very good level of flexibility in working with Java generics at run-time: during the compilation, the compiler specializes the generic code to enforce the type-safety, so all the information is lost. Reflection APIs allow to programmatically work on instances of generics at run-time, for example by checking the assignability of one instance to another.
There’s no code in this blog, simply because an extensive set of example is provided here in GuavaExplained by Google. The aim of this post was providing the reader with a more detailed understanding of what really Guava is, in terms of supports and facilities.