Guava

Language: Java

Utilities/Collections

Guava was created by Google to simplify common programming tasks in Java and to provide a high-quality, well-tested library for developers. It extends the capabilities of the standard Java library with immutable collections, advanced collection utilities, caching frameworks, functional idioms, and helper classes for strings, I/O, concurrency, and more. Guava is widely used in enterprise applications, open-source projects, and internal Google projects.

Guava is a set of core Java libraries developed by Google. It provides powerful utilities for collections, caching, concurrency, string manipulation, I/O, hashing, functional programming, and more, enhancing productivity and code readability.

Installation

maven: Add dependency in pom.xml: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.1.2-jre</version> </dependency>
gradle: Add dependency in build.gradle: implementation 'com.google.guava:guava:32.1.2-jre'

Usage

Guava provides utilities for collections, caching, functional programming, string operations, concurrency, hashing, I/O, and more. Key features include immutable collections, Multimap, BiMap, Range, caching with CacheBuilder, and functional idioms using Predicates, Functions, and Suppliers.

Immutable List

import com.google.common.collect.ImmutableList;

ImmutableList<String> list = ImmutableList.of("A", "B", "C");
System.out.println(list);

Creates an immutable list that cannot be modified after creation.

Multimap usage

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("fruit", "apple");
multimap.put("fruit", "banana");
System.out.println(multimap.get("fruit"));

Stores multiple values for a single key using a Multimap.

BiMap for bidirectional mapping

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;

BiMap<Integer, String> bimap = HashBiMap.create();
bimap.put(1, "one");
bimap.put(2, "two");
System.out.println(bimap.inverse().get("one"));

Allows bidirectional lookup between keys and values.

Caching with CacheBuilder

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

Cache<String, String> cache = CacheBuilder.newBuilder()
    .maximumSize(100)
    .build();
cache.put("key1", "value1");
System.out.println(cache.getIfPresent("key1"));

Demonstrates in-memory caching with eviction policies using Guava Cache.

String utilities

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

String joined = Joiner.on(", ").join("A", "B", "C");
Iterable<String> split = Splitter.on(",").split("A,B,C");
System.out.println(joined);
System.out.println(split);

Shows string joining and splitting utilities provided by Guava.

Functional idioms

import com.google.common.base.Function;
import com.google.common.collect.Lists;

List<String> strings = Lists.newArrayList("a", "b", "c");
List<Integer> lengths = Lists.transform(strings, (String s) -> s.length());
System.out.println(lengths);

Applies a function to transform elements of a collection.

Range usage

import com.google.common.collect.Range;

Range<Integer> range = Range.closed(1, 10);
System.out.println(range.contains(5));

Represents a range of values and checks if a number is within the range.

Error Handling

UnsupportedOperationException: Occurs when attempting to modify immutable collections. Always use ImmutableList/Set/Map appropriately.
NullPointerException: Guava’s immutable collections and utilities often disallow nulls. Check inputs before adding to collections.

Best Practices

Use immutable collections for thread safety and readability.

Leverage caching for expensive computations to improve performance.

Use functional idioms (Predicates, Functions) to simplify collection transformations.

Apply Guava utilities like Joiner, Splitter, and CharMatcher for clean string manipulation.

Avoid overusing Guava when standard Java 8+ streams and collections are sufficient.