Java String Join

When we are programming with String, or doing String manipulation in short, a typical problem we encounter is we wish to concatenate together some Strings using a specific delimiter. For example, if we have an array of 4 String representing fruits [Apple, Banana, Orange, Grapes], and wish to concatenate them using delimiter dash "-", the resulting String we want is "Apple-Banana-Orange-Grapes". Good thing we don't need to implement our own, nor we don't need an external library from Apache or Google. There is already a method we can use inside Java - String Join. Below are some examples on how we could concatenate a list of String using a specific delimiter using Java String Join. Note that this feature only exists from Java 8 and above.

Java String Join Syntax

In the Java String class, there are two signature we can find for Java String Join. Below are the method definition:
public static String join(CharSequence delimiter, CharSequence... elements)
The first thing we will notice is that this is a static method. It means we don't need an instance of String to call this from. We can just invoke it using the String class. We can also see that the first parameter is the delimiter we wish to use to separate the String. Then followed by the Character sequence we want to concatenate together. The result is the concatenated String, hence the result type is a String.
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
This is the second signature of the method. Again,it is a static method similar to the first method. It also mean we don't need an instance of String to call this from. We can also just invoke this from the class String. The first parameter is also the delimiter we wish to use to separate the String. The difference is what follows. Instead of CharSequence, we can supply it with Iterables. For example, we can call this and pass a List. The result is the concatenated String also. So the two signature are almost identical, except on what we want to concatenate. is it a CharSequence or iterable?

Java String Join CharSequence

Let's explore the first signature of the method Join found in the String class. Below is an example:

String joinedStr=String.join(",","Rock", "Paper", "Scissors");  
System.out.println(joinedStr);  

As shown above, we wish to use comma as the delimiter - the first parameter. The following Strings are those we want to concatenate. So they are concatenated together using comma. The comma is only used in between two items. There is no comma at the beginning nor at the end. To make it clear, below is the expected output:
Rock,Paper,Scissors

Java String Join ArrayList

The above code seems only to work when we know the exact Strings we want to concatenate. But what if they will come from a file or user input and we don't know what they are or how many? The answer is we can store them on an ArrayList and then use the second signature of String Join method in Java. Below is an example:

List<String> myList = new ArrayList<String>();
myList.add("Bike");
myList.add("Car");
myList.add("Truck");
String joinedStr = String.join(",", myList);
System.out.println(joinedStr);  

We created a list with three items on it. And then called the join method passing comma as delimiter and the ArrayList we created. Below is the output:

Bike,Car,Truck

Java String Join LinkedList

If we wish to use String Join method over a list, we can also use other implementation of List. For example, below is an example on how to use String Join on a LinkedList.
List<String> myList = new LinkedList<String>();
myList.add("Bike");
myList.add("Car");
myList.add("Truck");
String joinedStr = String.join(",", myList);
System.out.println(joinedStr);  

And the output will be the same as the example above:

Bike,Car,Truck

Java String Join Set

We are not limited to List when using the second signature. We can also pass a Set of String when calling Java String Join. Below is an example:

Set<String> myList = new LinkedHashSet<String>();
myList.add("Bike");
myList.add("Car");
myList.add("Truck");
myList.add("Car");
myList.add("Bike");
myList.add("Truck");
String joinedStr = String.join(",", myList);
System.out.println(joinedStr);  

In the example above, we added 6 items to the collection of String. But since this is a Set, the data structure only holds unique values. So in reality, the set will only contain three Strings. Hence the output below is expected.

Bike,Car,Truck

Java String Join Delimiters

Below are some play around on what delimiters we can use. For example, we can use "/" to format date, given that we know the month, day, and year. See below:

String date = String.join("/","26","07","2019");    
System.out.print(date);

The output is below, where the components of a date are concatenated using "/". This is a quick way of formatting dates in our program.

26/07/2019

Another example is using dash. For example, if we are concatenating words in a title of a blog post to create a hyperlink component.
String uri = String.join("-","java","is","great","and","fun");    
System.out.print("http://javadevnotes.com/"+uri);
The output is below.
http://javadevnotes.com/java-is-great-and-fun
We are not limited to one character delimiter. For example, we can use words like "stop".
String concatString = String.join("stop","JOHN","IS","COMING");    
System.out.print(concatString);

And the words given are now separated with the given delimiter stop, and returned as a String. Below is the output:

JOHNstopISstopCOMING