C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Result: This is a String. The result of Joiner is the same as an equivalent call to join(), part of the standard library.
JoinArray: The last part of the Joiner statement, join, can receive an Iterable (like an ArrayList) or an Object array. It returns a String.
ArrayListJava 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
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
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
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
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