Part of Guava, it offers the ability to round-trip data extracted with Splitter. We can append to a StringBuilder, or represent key-value stores in Strings.
Initial example. The "on" method receives the delimiter to place in between strings. The actual join() call receives a String array, Object array or Iterable.
import com.google.common.base.Joiner;
public class Program {
public static void main(String[] args) {
String[] values = { "one", "two", "three" };
// Use Joiner to combine all elements.// ... Specify delimiter in on method.
String result = Joiner.on("...").join(values);
System.out.println(result);
}
}one...two...three
SkipNulls. Unlike the standard join method, we can filter elements with a Joiner. With skipNulls, null elements in an array or Iterable are removed. Often null elements are not needed.
Here The null element, which in where "three" should be, was eliminated from the final output string.
Tip With the standard string join method, complex logic to remove null elements would be required. Guava's Joiner makes this simpler.
import com.google.common.base.Joiner;
public class Program {
public static void main(String[] args) {
String[] elements = { "one", "two", null, "four" };
// Call skipNulls.
String result = Joiner.on('+').skipNulls().join(elements);
System.out.println(result);
}
}one+two+four
UseForNull. Sometimes we want to substitute another value for null. This means null elements are not forgotten, and they can be represented in text (and persisted on disk).
Note This program changes nulls to question marks. I recommend using the literal "null" if that has no other uses, or -1 for numeric data.
import java.util.ArrayList;
import com.google.common.base.Joiner;
public class Program {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("cat");
list.add(null);
list.add(null);
list.add("dog");
// Invoke useForNull.
String value = Joiner.on(' ').useForNull("?").join(list);
System.out.println(value);
}
}cat ? ? dog
AppendTo. We can append to a StringBuilder with the Joiner class. First we call the static method on. Then we invoke appendTo, which returns StringBuilder instance.
Argument 1 This is the StringBuilder instance we want to append the values to. The StringBuilder keeps any existing data.
Argument 2 This argument holds the elements we wish to append. Here we use a String array. Collections like ArrayLists may be used.
import com.google.common.base.Joiner;
public class Program {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("Equipment: ");
String[] items = { "box", "mirror", "rope" };
// Append the String array to a StringBuilder.
StringBuilder value = Joiner.on("; ").appendTo(builder, items);
System.out.println(value);
}
}Equipment: box; mirror; rope
WithKeyValueSeparator. This method creates a String that holds key-value pairs. So we can convert a HashMap to a String with it. With "on," we specify the separator between pairs.
And With withKeyValueSeparator we pass the separator that is placed between the key and value of each pair.
Tip A method on Splitter, with the same name (withKeyValueSeparator), can be used to split apart these strings.
import java.util.HashMap;
import com.google.common.base.Joiner;
public class Program {
public static void main(String[] args) {
HashMap<String, String> hash = new HashMap<>();
hash.put("sky", "blue");
hash.put("fire", "red");
hash.put("grass", "green");
// Join together keys and values into a single String.
String result = Joiner.on(';').withKeyValueSeparator("=").join(hash);
System.out.println(result);
}
}sky=blue;grass=green;fire=red
In my experience, joining is less problematic than splitting strings. But it too has complexity. Null strings and key-value pairs must be handled.
Joiner, part of the Guava extensions to Java, is a perfect complement to Splitter. We can develop code that round-trips data with just calls to Splitter and Joiner. This is valuable.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.