Introduction to Google Guava's Joiner class

The Joiner class performs the complementary function of the Splitter class. The Joiner class will join an Iterable, an array, or a variable argument list using a given separator in between them.

Starting with the basics

The basic usage of the Joiner uses the on function and then by calling join with an array or Iterator object. To demonstrate this, let us use a Set of names to create a comma separated list.

Dealing with nulls

Like the Splitter, the Joiner comes with a couple of utilities for dealing with null in the input. The first is the skipNulls function that will return a Joiner which will automatically skip over any nulls it encounters when iterating over the elements. The second is the useForNull function which will use the parameter it was given in place of nulls. Here are examples of using each one:

If you use both the skipNulls and useForNull on the same joiner, it will throw an UnsupportedOperationException.

Using the Joiner class to build up a StringBuilder

In addition to joining a String all at the same time, you can build it up instead using a StringBuilder. The appendTo function of the Joiner takes the same arguments as the join function: an Iterable, array, or argument list an will work for both StringBuilders and any Appendable object. One caveat of this function is that it will only add the separator between elements within each function call. Below is an example of using it with a StringBuilder.

As you can see, the commas were only added in between elements in the same appendTo call which is why johndanmatt was not separated.

Using the MapJoiner (new in Guava 10.0)

A good example of using a MapJoiner is to create a query string out of a Map of query parameters. Let us use the following map:

{"param": "v", "p2": "v2", "q": "java"}

And the code:

The MapJoiner does not have utilities for trimming and cleaning up the strings, so that will have to be done while building the Map. You can read the full documentation on the Joiner class here.

View the previous post on Google Guava’s Splitter class

Introduction to Google Guava's Splitter class

The Splitter class takes a common, and what should not have to be verbose, exercise and makes it easy and readable. Let us start with a scenario, you have a list of key value pairs that you need to parse into a map. We are also going to assume the input could be malformed with empty entries and extra spaces. For the sake of this example, let us use names and user ids.

"dave:123, john:314,, matt:989"

Now, how this is typically handled:

This ends up being rather verbose and also error prone. We will see how the Splitter class makes this much easier and readable.

Starting with the basics

The simplest example of the Splitter is parsing a standard comma separated list.

Pretty straightforward. In addition to a string separator, the Splitter.on function can also use a char, CharSequence, or a Pattern. The API provides a convenience method Splitter.onPattern that will turn a String into a java Pattern.

Utilities to sanitize the input

We saw in the initial scenario a poorly formatted string that required us to check for null and trim at various points to clean up the input. The Splitter class comes with the utility functions omitEmptyStrings and trimResults to take away some of that boilerplate code.

Another benefit of using the utility functions to clean up the split string is readability. When someone is reading your code, it is much easier to understand what is happening when seeing omitEmptyStrings and trimResults versus checks for null, checks for empty string, and various trim calls.

Using the MapSplitter (new in Guava 10.0)

The MapSplitter allows you to take a list of key value pairs and easily turn it into a Java Map. Well, that sounds great for solving our initial scenario, let us see it in action.

That looks much better. We removed the loop, the input was sanitized, we threw out any empty strings, and we broke up the separated values into key/value pairs. It also reads a lot better:

Split a string on comma, omit empty strings, trim those results, then use colon as a key value separator. With these rules, split stringToSplit

This is much easier to read than our initial version. You can read the full documentation on the Splitter class here.