Guava Splitter

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 Strings. It is aspiring in replacing the usage of String.split method.

Its usage when we want to get back an Iterable of Strings 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s