Fundamentals 10 min read

Unlock Java Power: Master Guava’s Joiner, Splitter, CharMatcher & More

This article explores Google Guava’s most useful utilities—including Joiner, Splitter, CharMatcher, Multiset, Immutable collections, Multimap, BiMap, Table, Functions, Predicate, and Cache—showing how they simplify string handling, collection manipulation, caching, and asynchronous callbacks, with practical code examples and performance insights.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Unlock Java Power: Master Guava’s Joiner, Splitter, CharMatcher & More

Preface

Alibaba's Java coding standards are revered, but the author's company forbids using them.

Google Guava is widely used; this blog shares the author's experience with Guava, promising more enjoyable and elegant Java coding.

Object‑Oriented String Handling

Joiner / Splitter / CharMatcher

JDK's String API can be cumbersome; Guava offers Joiner and Splitter to simplify concatenation and splitting.

Joiner / Splitter

Example of Joiner and Splitter usage:

Joiner connects strings, Splitter splits them; both can be defined as static final and are well‑tested, more efficient than Apache Commons.

Common Joiner methods: skipNulls() , useForNull(String) .

Common Splitter methods: trimResults() , omitEmptyStrings() ; supports regex and fixed‑length splitting.

CharMatcher

Guava also provides CharMatcher for flexible character matching and processing.

It extends JDK primitive operations, offering utilities for Bytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans.

Guava Collections Enhancements

Multiset

Multiset is an unordered collection that allows duplicate elements, filling the gap between List and Set.

Immutable vs Unmodifiable

Unmodifiable collections are views of the original mutable collection; changes to the source affect the view.

Guava’s Immutable collections provide true immutability and thread‑safety via copy‑on‑write (e.g., ImmutableList, ImmutableSet, ImmutableMap).

Multimap

Multimap supports one key mapping to multiple values, avoiding manual Map<k, List<v>> handling.

All Guava collections have a create method to simplify generic declarations.

BiMap

BiMap provides a bidirectional map where both keys and values are unique, allowing lookup in both directions.

inverse() returns the reverse view; inverse().inverse() returns the original map.

Table

Table models a two‑dimensional map with rowKey , columnKey , and value , simplifying composite‑key scenarios.

Functional Programming Utilities

Functions

Functions enable transformation of collections, such as truncating strings and converting to uppercase.

Predicate

Predicate is commonly used for filtering collections; Guava provides Collections2.filter as an alternative to a non‑existent List.filter method.

Null‑Handling and Preconditions

Guava uses Preconditions.checkNotNull to fail fast on null arguments.

Cache

Guava’s local Cache offers a simple, efficient, thread‑safe caching solution without external services.

Asynchronous Callbacks

Guava can decorate JDK executors to add listener callbacks, simplifying asynchronous programming.

Summary

The article only scratches the surface of Guava; many more features exist, such as reflection, annotations, networking, concurrency, and I/O.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavacachingGuavafunctional programmingCollectionsString Utilities
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.