How to use join method of String class?

1. Introduction

In this article, we will go through the new methods added in String class in Java 8 under package java.lang.

A little about String class in Java. String class represents character strings. We cannot change the value of String once they we created it hence rendering the class String immutable. Before Java 8, if we had to join the strings stored in an array or a collection, we would traverse the elements stored in container, i.e. array or collection, and possibly use some kind of mutual container like StringBuilder or StringBuffer to append the strings. Or we would have used a third party library like Guava to achieve our goal. Although Guava offers better class and methods to work with Strings, we can achieve this task using JDK.

2. Content

2 new methods were added to String class in Java 8. Both of this method’s usage is to join the strings of varargs, array or collection into a new string using a delimiter. Let’s look at these methods.

3. String.join using varargs

Method signature

public static String join(
                CharSequence delimiter, 
                CharSequence... elements)

join method is an overloaded method in String class. The above mentioned version takes argument delimiter of type CharSequence and second argument is varargs of elements that should be joined. Below are a few different ways you can use the join method that accepts the varargs. If delimiter or elements is null, then NullPointerException is thrown.

String[] str = { "John", "Jane", "Lary" };
String result = String.join(", ", str);

Assert.assertEquals("John, Jane, Lary", result);

String result = String.join(", ", "John", "Jane", "Lary");

Assert.assertEquals("John, Jane, Lary", result);

If the element in varargs is null, then “null” is added.

String result = String.join(", ", "John", null, "Jane", "Lary");

Assert.assertEquals("John, null, Jane, Lary", result);

4. String.join using Iterable

Method signature

public static String join(
                    CharSequence delimiter,
                    Iterable<? extends CharSequence> elements)

This version of the join method takes delimiter and Iterable as an argument. Any Collection<E> can be added as an argument to Iterable as Collection<E> interface extends Iterable<E> interface. Remember, the order in which elements are joined is not guaranteed as the order of traversal of elements depends on underlying collection.

If delimiter or elements is null, then NullPointerException is thrown.

List<String> list = Arrays.asList("John", "Jane", "Lary");

String result = String.join(", ", list);

Assert.assertEquals("John, Jane, Lary", result);

Using Set.

Set<String> set = new LinkedHashSet<>();
set.add("John");
set.add("Jane");
set.add("Lary");
 
String result = String.join(", ", set);

Assert.assertEquals("John, Jane, Lary", result);

If the individual element is null, then “null” is added.

Set<String> set = new LinkedHashSet<>();
set.add("John");
set.add("Jane");
set.add(null);
set.add("Lary");
 
String result = String.join(", ", set);

Assert.assertEquals("John, Jane, null, Lary", result);

5. Conclusion

In this article, I showed the usage of 2 new methods added in String class. If you would like to explore more APIs related to String, please checkout the Google Guava APIs, i.e. Joiner and Splitter class.

Leave a Reply

Your email address will not be published. Required fields are marked *