In this post, we will learn how to use the Joiner.on() method in Java, which is one of the most commonly used methods of the Joiner class. We will also see some of the advantages and disadvantages of using this method, and how it compares with other ways to join strings and objects in Java.

1. Overview of Joiner.on() method

The Joiner.on() method is a static factory method that returns a new Joiner instance with the specified separator. The separator can be either a char or a String. This creates a new Joiner instance that uses a comma as the separator. You can then use this joiner instance to join various types of inputs, such as an array, iterable, varargs, or a map of key-value pairs. You can use it to join a list of names into a comma-separated string, or a map of key-value pairs into an equal-sign-separated string. For example:

Download Code

 
As you can see, the joiner instance can be reused for different types of inputs, as long as they are compatible with the separator. Another useful feature of the Joiner class is that it allows you to join maps of key-value pairs into one string, using different separators for the keys and the values. To do this, you need to use the withKeyValueSeparator() method, which returns a new Joiner.MapJoiner instance with a specified separator for the keys and the values. For example:

Download Code

 
The MapJoiner class inherits all the methods of the Joiner class, so you can also use skipNulls() and useForNull(String) to handle null values in the map. For example:

Download Code

2. Advantages and Disadvantages of Joiner.on() method

The major advantage of the Joiner.on() method is that it handles null values gracefully. You can either skip null values using the skipNulls() method, or replace them with a default value using the useForNull() method. It provides a fluent and chainable API that makes the code more readable and expressive. That means you can easily combine different methods of the Joiner class to achieve complex joining operations. For example:

Download Code

 
However, theJoiner.on() method is not thread-safe. The Joiner class is immutable, but it does not guarantee thread-safety for its inputs. If you are using the joiner instance in a concurrent environment, you need to ensure that the inputs are not modified by other threads while joining them.

3. Comparing with other methods in Java

The Joiner.on() method is not the only way to join strings and objects in Java. There are other ways to achieve the same task, such as the String.join() method, which is a built-in method since Java 8 that can join an array or an iterable of strings using a separator. For example:

Download Code

 
However, it do not provide any option to replace null values with a default value. You will get an unwanted "null" string in your output. It also does not allow you to join maps of key-value pairs. You can also use the StringBuilder class with a for loop to achieve this task. It gives you full control over the logic and the output. However, it also requires more boilerplate code and error handling, and it is less readable and expressive than the Joiner.on() method.

4. Conclusion

In this post, we have learned how to use the Joiner.on() method in Java, which is a powerful and convenient way to join strings and objects using a separator. We have seen how to use some other methods of the Joiner class to customize the join operation and handle null values. We have also seen some of the advantages and disadvantages of using this method, and how it compares with other ways to join strings and objects in Java.

For more information about this method, you can check out the Guava official documentation or its GitHub repository.