Java Joiner Examples: join

Use the Joiner class to join strings. Specify 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:
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.


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

Java program that uses Joiner, join import; 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; 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; 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; 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; 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-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls