Guava is an omnibus open source Java library created by Google for its internal needs. Within this library, you’ll find a treasure chest of functionality, all of which is actively maintained by Google. Due to Guava’s wide adoption within the company and the larger community, the library has grown steadily: The current version is its 30th major release.
Overview of the functionality
In Java 6, the library offered preconditions. These methods, which are still in Guava, let you check the preconditions in a method prior to calling a given method.
Guava offers other similar precondition tests, such as checkArgument() and checkPositionIndex(). The latter call, by default, checks for a value between 0 and the maximum size of the given array. Java SE ultimately added similar capability. In Java 7, the Objects class provided a similar set of checks as static methods. In Java 9, the number of test methods was increased significantly.
Collections
Guava grew out of Google Collections, so the set of collections it provides is deep indeed. Since the early days of Java’s Collections class, you could use unmodifiable options, such as Collections.unmodifiableSet and its siblings: list and map. However, it wasn’t until Java 9 that you could create these data structures using fluid method calls such as Map.of, and it was not until Java 10 that you could enjoy Map.copyOf. Guava has offered this kind of functionality for many releases, and so if you’re forced to use releases earlier than Java 10, Guava gives you a solution—and a lot more.
Guava provides immutable variants of all the standard collections (set, list, table, and so on), and it adds some additional, very handy data structures. The first of these is the BiMap, or bidirectional map. A BiMap solves a common problem that occurs with standard maps that are built on conventional key-value pairs: Sometimes you need the values to be keys and the keys to be values. With the BiMap, each entry serves as a key-value pair that can be reversed, so you can use the value to point to a key. The only requirement is that all keys and values be unique. A related and equally useful data structure in Guava is the multimap. This construct solves a problem that occurs routinely in Java programming: a key-value map in which the value is a collection of some kind, such as a list.
Lightweight cache
Guava provides several lightweight versions of features typically used in enterprise applications, including a cache and a publish-subscribe (pub-sub) solution. The cache works as a classic key-value store and cache values are returned if they are present; otherwise, they are computed via callable methods and inserted into the cache. You can configure the eviction method to your preference. Options include eviction based on size, on the absence of references, or on timing. Left to its own devices, the cache evicts on a least-recently used (LRU) basis t. You can manually evict individual entries.
The Guava cache does not use a thread in the background constantly monitoring entries looking for which ones to clean up. It does that maintenance work only on insertions and on reads, meaning that, for example, items can remain in the cache even after their allotted time has expired or when there are no other references to them. Eventually, if there is memory pressure on the cache, those items will be collected and removed. However, if you need a different eviction scheme, you can use the Guava cache API to add your own preferred eviction mechanism.
Extensions to Java primitive data types
In Java’s primitive data types there are no unsigned integers. Guava provides unsigned bytes, integers, and longs. It also provides boxed wrappers for these data types and thoughtfully includes the basic functions you’d expect in support of these types: minimum, maximum, and comparison functions; conversion to BigDecimal; as well as, toString().