On this page
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:
com.google.common.base.Optional→ preferjava.util.Optional.com.google.common.base.Function/Predicate→ preferjava.util.function.*.Lists.newArrayList(...)/ImmutableList.of(...)→List.of(...)andList.copyOf(...)cover most basic immutable needs (though Guava'sImmutableListtype communicates immutability in method signatures).com.google.common.io.Files→ preferjava.nio.file.Files.
Linked from
Related notes
- tuts/third_party/java/apache_commons Linked here; links to this page; shared tags: #java, #prog, #tier, #utilities
- tuts/third_party/java/jackson Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java Linked here; links to this page; shared tags: #java, #prog
- tuts/java links to this page; shared tags: #java, #prog
- tuts/java/stdlib/java_util Linked here; shared tags: #java, #prog
- tuts/java/stdlib/java_util_concurrent Linked here; shared tags: #java, #prog
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #prog, #tier
- tuts/java/builtins/AutoCloseable shared tags: #java, #prog, #tier
- tuts/java/builtins/enum shared tags: #java, #prog, #tier
- tuts/java/builtins/final shared tags: #java, #prog, #tier