This post will discuss how to create toString() method for any object in Java using Guava MoreObjects.ToStringHelper() method.

The toString() method in Java returns the string representation of an object, which can be useful for debugging, logging, or displaying information to users. It is very important to implement a meaningful toString() method for your classes. However, writing a good toString() method can be tedious and error-prone, especially if your class has many fields or complex data structures. Fortunately, there is a handy utility class in Google Guava library that can help you with that: MoreObjects.ToStringHelper().

1. Overview of MoreObjects.ToStringHelper() method

Guava MoreObjects.ToStringHelper() is a static factory method that returns an instance of MoreObjects.ToStringHelper class. This class provides a fluent API to build a string representation of an object using key-value pairs. You can use this class to implement the toString() method of your class. The basic usage of this method is as follows:

 
In this example, we pass the current object (this) to the toStringHelper() method, which returns an instance of ToStringHelper. Then we chain the add() method calls for each of the fields in the class along with their value. Finally, we call the toString() method to get back the string representation. The output of this method has the format: ClassName{key1=value1, key2=value2, …}. The order of calling these methods is significant, as it determines the order of the fields in the output.

 
Some of the benefits and features of this method are:

  1. It automatically uses the simple name of the class as the prefix, unless you specify a different name using the toStringHelper(String) overload.
  2. It handles null values gracefully by omitting them from the output, unless you use the addValue() method instead of add().
  3. It supports arrays and collections by using Arrays.toString() and Collections.toString() methods internally.
  4. It allows you to customize the separator, omitNullValues, and skipNulls options using the respective methods in the ToStringHelper class.

2. Usage of MoreObjects.ToStringHelper() method

Let’s see some examples of how to use the Guava MoreObjects.ToStringHelper() method in Java with different scenarios and edge cases.

Example 1: Implementing toString() for a simple class

Let’s say we have a Student class. Each student has a name, id, age, and a list of courses. To create a clear and expressive string representation of our Student class, we pass the current object (this) to the toStringHelper() method, which returns an instance of ToStringHelper. Then we chain the add() method calls for each of the fields in the class along with their value. Finally, we call the toString() method to get back the string representation.

Download Code

 
As you can see, the output of the toString() method is concise and effective, showing the class name and the key-value pairs of the fields. It also handles the array and collection fields by internally using Arrays.toString() and Collections.toString() methods.

Example 2: Generating toString() without field name

Sometimes, you may want to generate a string representation of an object without showing the field name, but only the value. For example, you may want to show only the id of a Student object, or only the name of a Person object. In such cases, you can use the addValue() method instead of add() method in the ToStringHelper class. The addValue() method takes only one argument, which is the value to be added to the string. It does not show the field name in the output.

Let’s see how this works with some sample data. We have a simple Person class that has only one field: name. In the toString() method, we use the addValue() method to add only the name to the string, which does not show the field name in the output.

Download Code

 
As you can see, the output of the toString() method shows only the class name and the value of the name field. It does not show the key or the field name in the output.

Example 3: Handling null values

One of the advantages of using the Guava MoreObjects.ToStringHelper() method is that it can handles null values gracefully by omitting them from the output. This can be useful when you have optional fields in your class that may or may not have a value. By default, the output of the toString() method does not omit the null values from the output. To omit the null values from the output, you can use the omitNullValues() method in the ToStringHelper class. This method takes no arguments and returns a reference to the ToStringHelper instance.

For example, we have an Employee class that has four fields: name, id, department, and manager. The department and manager fields are optional and may be null for some employees. In order to skip the optional fields when they are null, we call the omitNullValues() method to the ToStringHelper instance.

Download Code

That’s all about Guava MoreObjects.ToStringHelper() method in Java.