Unravel โบ Java concepts โบ Libraries and tooling
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
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.
- 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.
- 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.
- 3Joiner.on(", ").skipNulls().join(list) and Splitter.on(',').trimResults().omitEmptyStrings().split(text) handle the edge cases String.join and split do not
- 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.
- Guava is a big, widely used dependency; versions clash on the class path (an old Guava from another library wins)
- For new code consider: JDK immutable collections and streams for the basics, and Caffeine instead of the Guava cache (the successor with better performance)
- 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
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); }}- line 5 uses ImmutableList, the core of Guava utilities
- line 6 uses Joiner, the core of Guava utilities
- line 7 uses Splitter, the core of Guava utilities
- line 8 uses Multimap, the core of Guava utilities
- line 9 uses Multimap, the core of Guava utilities
- 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
ImmutableListto 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:
- Types:
ImmutableListImmutableMapImmutableSetListsMapsSetsJoinerSplitterPreconditionsMultimapArrayListMultimapCacheBuilderLoadingCacheStopwatchand 11 more
Part of a learning path
- JDBC and everyday libraries ยท previous: ResultSet and mapping rows to objects ยท next: Apache Commons helpers
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.