BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Libraries and tooling

Java language

Guava utilities

Guava is Google's utility library: immutable collections (ImmutableList.of), richer collections (Multimap, BiMap, Table), string helpers (Joiner, Splitter), Preconditions and a simple in-memory cache.

Learn these first

At a glance

Mental model

A well-stocked toolbox of the small tools the standard kit forgot: each one saves a few lines of code you would otherwise write (and test) yourself.

What happens, in order
  1. 1ImmutableList.of(1, 2, 3), ImmutableMap.of("a", 1), ImmutableSet.copyOf(x): immutable, null-hostile collections that fail fast on a null elementThe JDK now has List.of and List.copyOf too.
  2. 2Multimap<K, V> is a map from a key to several values: ArrayListMultimap.create().put("fruit", "apple") replaces Map<K, List<V>> with computeIfAbsentBiMap keeps keys and values unique both ways, Table is a map with two keys, Multiset counts occurrences.
  3. 3Joiner.on(", ").skipNulls().join(list) and Splitter.on(',').trimResults().omitEmptyStrings().split(text) handle the edge cases String.join and split do not
  4. 4Preconditions.checkArgument(x > 0, "x must be positive but was %s", x) validates argumentsCacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES).build(loader) builds a loading cache.
Remember
Watch out
Expecting ImmutableList to accept nullsUsing the Guava cache without a size limitMixing several Guava versions on the class path
Example (highlighted lines explain it)
import com.google.common.base.*;import com.google.common.collect.*;import java.util.List;public class Menu {  static final ImmutableList<String> DAYS = ImmutableList.of("mon", "tue", "wed");  static String line(List<String> items) { return Joiner.on(", ").skipNulls().join(items); }  static Iterable<String> parts(String csv) { return Splitter.on(',').trimResults().omitEmptyStrings().split(csv); }  static Multimap<String, String> group() {    Multimap<String, String> m = ArrayListMultimap.create();    m.put("fruit", "apple"); m.put("fruit", "pear");    return m;  }  static void check(int n) { Preconditions.checkArgument(n > 0, "n must be positive but was %s", n); }}
  1. line 5 uses ImmutableList, the core of Guava utilities
  2. line 6 uses Joiner, the core of Guava utilities
  3. line 7 uses Splitter, the core of Guava utilities
  4. line 8 uses Multimap, the core of Guava utilities
  5. line 9 uses Multimap, the core of Guava utilities
  6. line 13 uses Preconditions, the core of Guava utilities
Read the full explanation (about 1 min)

Guava is Google's utility library: immutable collections (ImmutableList.of), richer collections (Multimap, BiMap, Table), string helpers (Joiner, Splitter), Preconditions and a simple in-memory cache. Much of it has since moved into the JDK.

A mental model

A well-stocked toolbox of the small tools the standard kit forgot: each one saves a few lines of code you would otherwise write (and test) yourself.

How it works

ImmutableList.of(1, 2, 3), ImmutableMap.of("a", 1), ImmutableSet.copyOf(x): immutable, null-hostile collections that fail fast on a null element. The JDK now has List.of and List.copyOf too.

Multimap<K, V> is a map from a key to several values: ArrayListMultimap.create().put("fruit", "apple") replaces Map<K, List<V>> with computeIfAbsent. BiMap keeps keys and values unique both ways, Table is a map with two keys, Multiset counts occurrences.

Joiner.on(", ").skipNulls().join(list) and Splitter.on(',').trimResults().omitEmptyStrings().split(text) handle the edge cases String.join and split do not.

Preconditions.checkArgument(x > 0, "x must be positive but was %s", x) validates arguments. CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(10, MINUTES).build(loader) builds a loading cache.

Going deeper

Guava is a big, widely used dependency; versions clash on the class path (an old Guava from another library wins). Check mvn dependency:tree when you get NoSuchMethodError.

For new code consider: JDK immutable collections and streams for the basics, and Caffeine instead of the Guava cache (the successor with better performance).

Annotated with @Beta or @VisibleForTesting signals API stability: do not depend on beta APIs in long-lived code.

Under the hood

ImmutableList has specialised implementations (empty, single element, regular array) and is a true immutable copy, not a wrapper, so there is no way to change it through another reference.

Common mistakes

  • Expecting ImmutableList to accept nulls.
  • Using the Guava cache without a size limit.
  • Mixing several Guava versions on the class path.

How Unravel recognises it

When you paste code, Unravel looks for these signs of guava utilities. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Guava utilities?

Guava is Google's utility library: immutable collections (ImmutableList.of), richer collections (Multimap, BiMap, Table), string helpers (Joiner, Splitter), Preconditions and a simple in-memory cache. Much of it has since moved into the JDK.

What are the most common mistakes with Guava utilities?

Expecting ImmutableList to accept nulls. Using the Guava cache without a size limit. Mixing several Guava versions on the class path.

How can I see Guava utilities in real code?

Paste your own Java into Unravel and it will point out where guava utilities appears, explain how it works at the detail level you choose, and draw the flow of the code around it.