05Oct2015

Java Development: Guava – beyond standard JDK

Every modern software development stack has one or more well-known common libraries which is not a part of the language core. On the other hand, such general purpose package extends standard set of methods and data structures to professional level of software development. E.g., STL for C++ world. Apache Commons [https://commons.apache.org] is one of the most useful set of general libraries in Java Development. A lot of other packages and frameworks are based on them. In case of new project, Iflexion developers, first of all, to look at Guava [https://code.google.com/p/guava-libraries] — a modern professional implementation of common methods.

Guava libraries

Guava is done by Google, freely distributed and supported by Internet giant. They even claim Guava usage in their own projects as a standard. There are 3 main programming languages are used in Google: Java, C++ and Python. Guava is based on Java 6. That makes it useful in legacy projects implemented in terms of Java 6 and needed to support this version. E.g., strings join may be done in Java 8 as String.join(“, “, strings) or by Guava Joiner.on(“, “).join(strings). Guava modules are created with design patterns in mind. There are builders, factory methods, adapters, etc. Guava available via maven/gradle. E.g., for gradle:

dependencies {

compile ‘com.google.guava:guava:18.0′

}

 Basics

Basic Object utilities like toString, hashCode, equals, would be customized much faster in compact Guava way. Besides, it helps to add precondition checks into the methods, play with null’s, throw and examine exceptions and errors. Guava provides routines for Java Development in more functional style even without Java 8 features.

Collections

Most useful part of Guava is Collections library for Java programming. As an alternative to Apache Commons, it provides more complicated data structures like BiMap, Multimap, Multiset, RangeMap, RangeSet, Table and static fabric methods for them. Guava Collections are based on generics and fully compatible with standard Java interfaces. Besides, it provides easy way to construct thread-safe data structures, by simple decoration, like Multimaps.synchronizedSetMultimap(HashMultimap.create()).

 String operations

All modern languages have string utilities like splitting, joining, padding, etc. Standard Java string operations provided by JDK are not powerful enough to cover such cases. Implementation of these string methods provided by Guava is much better alternative to proprietary routines at line of both safety and performance.

 Caching

In a variety of use cases, the value is very expensive in terms of its calculation. On the other hand, such value may be used a lot of times. Usually, it would be better to cache such values in some way. It means to use memory to improve speed. Guava provides elegant interface for building and customization cache structures. It’s pure local cache that have to be fit in RAM. A data is not going to file or another server.

Read also

Comments are closed.