Open In App

Guava Library in Java

Last Updated : 29 Mar, 2024
Like Article

Google Guava is an open-source(a decentralized software-development model that encourages open collaboration) set of common libraries for Java, mainly developed by Google engineers. It helps in reducing coding errors. It provides utility methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and validations. The most recent release is Guava 25.0, released 2018-04-25.

Why Guava ?

  • By replacing the existing library classes with those from guava, you can reduce the amount of code you need to maintain.
  • It is a reliable, fast, and efficient.
  • It provides many utility classes like Iterables, Lists, Sets, Maps, Multisets, Multimaps, Tables which are regularly required in programming application development.
  • Many Guava utilities reject and fail fast on nulls, rather than accepting them blindly, as null can be ambiguous sometimes.
  • It simplifies implementing Object methods, like hashCode() and toString().
  • Guava provides the Preconditions class with a series of common preconditions.
  • The Guava library is highly optimized.
  • It simplifies propagating and examining exceptions and errors with help of Throwables utility.
  • Guava’s powerful API helps in dealing with ranges on Comparable types, both continuous and discrete.
  • It provides tools for more sophisticated hashes than what’s provided by Object.hashCode(), including Bloom filters.
  • It provides Optimized, thoroughly tested math utilities not provided by the JDK.
  • Guava provides a few extremely useful string utilities like splitting, joining, padding, and more.
  • It provides powerful collection utilities, for common operations not provided in java.util.Collections.
  • and many more….

Example : As we know the primitive types of Java are the basic types : byte, short, int, long, float, double, char, boolean. These types cannot be used as objects or as type parameters to generic types, which means that many general-purpose utilities cannot be applied to them. Guava provides a number of these general-purpose utilities, ways of interfacing between primitive arrays and collection APIs, conversion from types to byte array representations, and support for unsigned behaviors on certain types.

Let us have an overview of utilities and classes that Guava provides over existing library classes.

  1. Optional Class : Optional object is used to represent null with absent value. Many of the cases where programmers use null is to indicate some sort of absence, perhaps where there might have been a value, but there is none, or the value could not be found. Optional<T> is a way of replacing a nullable T reference with a non-null value.
    An Optional may either contain a non-null T reference i.e, the case we say the reference is “present”, or it may contain nothing i.e, the case we say the reference is “absent”. It is never said to “contain null.”
  2. Preconditions Class : Guava provides a number of precondition checking utilities. Preconditions provide static methods to check that a method or a constructor is invoked with proper parameter or not. Each method has three variants :
    • No extra arguments.
    • An extra Object argument.
    • An extra String argument, with an arbitrary number of additional Object arguments.

    After static imports, the Guava methods are clear and unambiguous.

  3. Ordering Class : Ordering is Guava’s “fluent” Comparator class, which can be used to build complex comparators and apply them to collections of objects. For additional power, Ordering class provides chaining methods to tweak and enhance existing comparators.
  4. Objects Class : Objects class provides helper functions applicable to all objects such as equals, hashCode, toString, compare/compareTo.
  5. Throwables : Throwables class provides utility methods related to Throwable interface. Sometimes, when you catch an exception, you want to throw it back up to the next try/catch block. This is frequently the case for RuntimeException or Error instances, which do not require try/catch blocks, but can be caught by try/catch blocks when you don’t mean them to. Guava provides several utilities to simplify propagating exceptions.
  6. Collection Utilities : Guava introduces many advanced collections. These are among the most popular and mature parts of Guava. Some of useful collections provided by Guava are : Multiset, Multimap, BiMap, Table, ClassToInstanceMap, RangeSet, RangeMap.
  7. Graphs : Guava’s common.graph is a library for modeling graph-structured data, that is, entities and the relationships between them. Some examples can be :
    • Webpages and hyperlinks.
    • Airports and the routes between them.
    • People and their family trees.
  8. String Utilities : Guava introduces many advanced string utilities such as Joiner, Splitter, CharMatcher, Charsets, CaseFormat.
  9. Primitive Utilities : As primitive types of Java cannot be used to pass in generics or in collections as input, Guava provided a lot of Wrapper Utilities classes to handle primitive types as Objects.
  10. Math Utilities : Guava provides Mathematics related Utilities classes to handle int, long and BigInteger. These utilities are already exhaustively tested for unusual overflow conditions. They have been benchmarked and optimized. They are designed to encourage readable, correct programming habits.
  11. Caches : Caches are tremendously useful in a wide variety of use cases. For example, you should consider using caches when a value is expensive to compute or retrieve, and you will need its value on a certain input more than once. A Cache is similar to ConcurrentMap, but not quite the same. Generally, the Guava caching utilities are applicable whenever :
    • You are willing to spend some memory to improve speed.
    • You expect that keys will sometimes get queried more than once.
    • Your cache will not need to store more data than what would fit in RAM.

To summarize the cool features of Guava, refer the table given below :

We will be discussing more in detail about these Classes and utilities in our future articles.

Reference : Google Guava

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads