Guava Splitter
is a utility class that is offering roughly speaking the opposite of what the Joiner
utility class does.
You can have a read at my previous quick article about Guava Joiner.
Splitter
is offering similar look-and-feel as Joiner
: the on
method serves as a static factory method, subsequent method calls are following the builder pattern returning a wrapped this
(therefore need to be packed prior the final split
method call). Finally it doesn’t try to be complicated returning an Iterable
of String
s. It is aspiring in replacing the usage of String.split
method.
Its usage when we want to get back an Iterable
of String
s looks like:
Splitter.on(",").trimResults().split("my,string,to,split")
When we want to perform the operation retrieving a Map
out of its String
representation:
Splitter.on(",").trimResults().omitEmptyStrings().withKeyValueSeparator("->").split("one->1,two->2,three->3")
In this instance the call to withKeyValueSeparator
is yielding a Splitter.MapSplitter
inner class that is adding the map context to the splitter.