Guava is an open source library containing many classes for Java and written by Google. It's a potentially useful source of miscellaneous utility functions and classes that I'm sure many developers have written themselves before, or maybe just wanted and never had time to write. Here's 5 good reasons to use it!
1. Collection Initializers and Utilities
Generic homogeneous collections are a great feature to have in Java, but sometimes their construction is a bit too verbose, for example:
final Map<String, Map<String, Integer>> lookup = new HashMap<String, Map<String, Integer>>();
Java 7 solves this problem in a really generic way, by allowing a limited form of type inference informally referred to as the
Diamond Operator. So we can rewrite the above example as:
final Map<String, Map<String, Integer>> lookup = new HashMap<>();
It's actually already possible to have this kind of inference for non-constructor methods in earlier Java releases, and Guava provides many ready made constructors for existing Java Collections. The above example can be written as:
final Map<String, Map<String, Integer>> lookup = Maps.newHashMap();
Guava also provides many useful utility functions for collections under the
Maps,
Sets et al. classes. Particular favourites of mine are the Sets.union and Sets.intersection methods that return views on the sets, rather than recomputing the values.