Guava for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Guava and why is it needed?

\n\n

Guava is a powerful open-source library from Google that provides a set of high-quality utilities for the Java language. It is designed to make Java code more concise, reliable, and performant by solving many common tasks that developers face.

\n\n

Why is Guava needed? The Java Standard Library (JDK) provides basic functionality but often leaves much to be desired in terms of convenience and safety. Guava fills these gaps by offering:

\n
    \n
  • Simplifying work with collections: Creating immutable collections, filtering, transforming, grouping — all of this becomes much easier.
  • \n
  • Caching: A powerful and flexible framework for in-memory data caching.
  • \n
  • Primitive handling: Utilities for working with arrays of primitive types (int, long, double, etc.).
  • \n
  • String operations: Convenient classes for working with strings, such as Joiner and Splitter.
  • \n
  • Working with functions and predicates: Functional programming style (though not as concise as in Java 8+, but still useful).
  • \n
  • Precondition checks (Preconditions): An elegant replacement for manual null checks and other conditions.
  • \n
  • Hashing: Utilities for creating hash functions.
  • \n
  • Streams and concurrency: Utilities for working with multithreading, including ListenableFuture.
  • \n
\n

If you write in Java and want to improve code quality and development speed, Guava is a must-have library.

\n\n

Installing Guava

\n\n

Guava is not installed via pip or npm, as it is a Java library. It is distributed through dependency management systems such as Maven or Gradle.

\n\n

Installation via Maven (pom.xml)

\n

Add the following code to the <dependencies> section of your pom.xml file:

\n
<dependency>\n    <groupId>com.google.guava</groupId>\n    <artifactId>guava</artifactId>\n    <version>33.2.0-jre</version> <!-- Use the latest version -->\n</dependency>
\n\n

Installation via Gradle (build.gradle)

\n

Add to the dependencies section:

\n
implementation 'com.google.guava:guava:33.2.0-jre'
\n\n

Manual installation (downloading JAR)

\n

You can also download the JAR file from the Guava GitHub releases and add it to your project's classpath.

\n\n

Important: Version 33.2.0-jre is intended for Java 8 and above. For older Java versions, use the android version (e.g., 33.2.0-android).

\n\n

Quick start with code example

\n\n

Let's write a simple program that demonstrates several key features of Guava.

\n\n
import com.google.common.base.Joiner;\nimport com.google.common.base.Splitter;\nimport com.google.common.collect.ImmutableList;\nimport com.google.common.collect.Lists;\n\nimport java.util.List;\n\npublic class GuavaQuickStart {\n    public static void main(String[] args) {\n        // 1. Creating an immutable list\n        ImmutableList<String> names = ImmutableList.of("Alice", "Bob", "Charlie");\n        System.out.println("Names: " + names); // [Alice, Bob, Charlie]\n\n        // 2. Joining strings using Joiner\n        String joined = Joiner.on(", ").join(names);\n        System.out.println("Joined names: " + joined); // Alice, Bob, Charlie\n\n        // 3. Splitting a string using Splitter\n        String csv = "apple,banana,,grape";\n        Iterable<String> fruits = Splitter.on(",")\n                .trimResults()\n                    

Recommendations