On this page
  1. Google Guava
  2. Overview
  3. High-Value Features Not in the JDK
  4. Common Usage Examples
  5. Guava vs. Modern JDK (Java 17–21)

Google Guava

Prev: Apache Commons | Up: third_party/java | Next: Jackson
Rel: java.util, java.util.concurrent, Apache Commons
Ref: Google Guava GitHub & Wiki | Maven Central
Tags: #public #prog #java #third-party #collections #utilities #tier/standard


Overview

Google Guava (com.google.guava:guava) is Google's core Java library, known for null-hostile immutable collections, rich multi-value collection types missing from the JDK, in-memory caching, graph structures, and concise precondition/string utilities.

High-Value Features Not in the JDK

Feature Types Why Use It
Multimap ListMultimap, SetMultimap, ImmutableListMultimap Maps keys to multiple values cleanly without Map<K, List<V>> boilerplate
BiMap HashBiMap, ImmutableBiMap Bidirectional 1-to-1 map with inverse() view and unique-value enforcement
Table HashBasedTable, ImmutableTable Two-key matrix ((row, col) -> value) instead of nested maps
Multiset HashMultiset, ImmutableMultiset Bag / frequency counter with count(elem)
Range & RangeMap Range<C>, TreeRangeMap Closed/open intervals (Range.closedOpen(0, 10)) and interval lookups
Graph Graph, ValueGraph, Network Clean directed/undirected graph data structures
Caching CacheBuilder, LoadingCache Bounded size/TTL in-memory cache (for ultra-high concurrency, also consider Caffeine)
Rate Limiting RateLimiter Token-bucket smooth rate limiter for client throttling

Common Usage Examples

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import java.util.List;

// 1. Preconditions with formatted messages
Preconditions.checkArgument(limit > 0, "limit must be positive, got %s", limit);

// 2. Predictable splitting (handles empty strings & trimming cleanly)
List<String> tags = Splitter.on(',')
    .trimResults()
    .omitEmptyStrings()
    .splitToList("java, , guava, jdk21,");

// 3. Multimap & BiMap
Multimap<String, String> byRole = ArrayListMultimap.create();
byRole.put("admin", "alice");
byRole.put("admin", "bob");

BiMap<String, Integer> codes = HashBiMap.create();
codes.put("OK", 200);
String label = codes.inverse().get(200); // "OK"

// 4. Immutable collections with explicit type guarantees
ImmutableList<String> copy = ImmutableList.copyOf(tags);

Guava vs. Modern JDK (Java 17–21)

Prefer the JDK for features that have been standardized:

Linked from

Related notes