Java Joiner Examples: join

This Java Guava article uses the Joiner class to join strings. It specifies a delimiter with the on method.
Joiner. This class provides advanced functionality for the join operation. As with Splitter, it has extensions and advanced functionality.Splitter
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.guava: github.com
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.String Arrays

Result: This is a String. The result of Joiner is the same as an equivalent call to join(), part of the standard library.

Join

Array: The last part of the Joiner statement, join, can receive an Iterable (like an ArrayList) or an Object array. It returns a String.

ArrayList
Java program that uses Joiner, join 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); } } Output 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.

Java program that uses skipNulls 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); } } Output 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.

Java program that invokes useForNull 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); } } Output 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.StringBuilder

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.

Java program that uses appendTo, StringBuilder 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); } } Output 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.HashMap

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.

Java program that uses withKeyValueSeparator 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); } } Output 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls