Guava
Google's core Java libraries — collections, caching, primitives and utilities.
What it is
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.
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.
- Licence
- Apache 2.0
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- You want immutable collections, a solid in-memory cache, or utilities the JDK never added
- An existing codebase already depends on it
Look elsewhere when
- Modern Java covers much of it — `List.of`, `Optional`, `String.join` and streams replaced whole sections
- You need only one helper; the dependency is large
Installation
Add dependency in pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.2-jre</version>
</dependency>Getting started
The smallest useful thing you can do with it, and what each part means.
import com.google.common.collect.ImmutableList;
ImmutableList<String> list = ImmutableList.of("A", "B", "C");
System.out.println(list);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"));Advanced usage
Where the library earns its place over a simpler alternative.
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"));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"));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);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);import com.google.common.collect.Range;
Range<Integer> range = Range.closed(1, 10);
System.out.println(range.contains(5));Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- 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.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
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.
