StringJoiner in java

StringJoiner in java is introduced in java 8 and it is a final class that exists in java.util package. The StringJoiner class provides an easy way to create a String by joining the multiple strings by a delimiter. We can also provide prefix and suffix to the string. The prefix and suffix are optionally, and we can ignore them also.  In this post, we will see how to use the StringJoiner in java and how to join multiple strings in an easy way.

Here is the table content of the article will we will cover this topic.
1. StringJoiner in java?
2. Need of StringJoiner class?
3. How to use the StringJoiner Java?
4. Methods of StringJoiner?

StringJoiner in java

Need of StringJoiner class

In java String literal or object is mostly used in every program. Java provides three classes to handle the strings those are String, StringBuffer, StringBuilder. But there are few things that are incomplete without StringJoiner class.

You might face a problem when you want to join multiple strings and separate them with a delimiter. In these types of situations, you need to do manual code to join each string with the delimiter. Because Java didn’t provide any way to join strings, so we write the loop structure and join all the strings. This solution works but we need to be careful because we don’t need a delimiter before the first element and after the last element. This is the main cause of the issue.

Every developer faces this type of problem in coding. So, it is a very common requirement for developers. Since Java 8, this problem has resolved because StringJoiner class removing the headache of the string joining concept.

Java 8 provides some ways to join the string, we can use the join() method of String or by use of java StringJoiner class.

How to use the StringJoiner Java?

There are two constructors that are used to create a String by use of multiple strings. There are some methods that help to join the strings. Here we will discuss the two constructors of StringJoiner.

1. StringJoiner(CharSequence delimiter)

This constructor creates an empty object of StringJoiner with a given delimiter. It takes only one parameter that will be the delimiter of strings. The given delimiter will not be added at the beginning and end of the string. It will throw NullPointerException, if given delimiter is null.

public StringJoiner(CharSequence delimiter) {
        this(delimiter, "", "");
    }
import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner string = new StringJoiner(",");
		string.add("Hi");
		string.add("All");
		string.add("This");
		string.add("is");
		string.add("JavaGoal");
		string.add("Learning");
		string.add("Website");
		
		System.out.println("String by StringJoiner: "+ string);
	}

}

Output: String by StringJoiner: Hi,All,This,is,JavaGoal,Learning,Website

2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

This constructor constructs an empty object of StringJoiner with no characters in it using copies of the supplied given prefix, delimiter and suffix. If there is no string added in the object it will contain the prefix and suffix in the object. It throws NullPointerException, if the delimiter, prefix or suffix is null.

It takes three parameters those are delimiter, prefix and suffix.  The delimiter is used between each string added to the StringJoiner. The prefix added at the beginning and the suffix added at the end.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner string = new StringJoiner(",", "[", "]");
		string.add("Hi");
		string.add("All");
		string.add("This");
		string.add("is");
		string.add("JavaGoal");
		string.add("Learning");
		string.add("Website");
		
		System.out.println("String by StringJoiner: "+ string);
	}

}

Output: String by StringJoiner: [Hi,All,This,is,JavaGoal,Learning,Website]

Methods of StringJoiner

There are a few methods used to handle the object of StringJoiner. Here we will discuss each method with example see how method working in java string join.

1. StringJoiner toString(): Every programmer knows the toString() method is defined in Object class. The StringJoiner class overrides it and provides its own implementation. The toString() method returns the object of String type. There may be a situation when you want to deal with the string objects rather than StringJoiner. For example, if you want to compare the values of String then you need to convert StringJoiner to a String object.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		String str = "JavaGoal is learning website";
		StringJoiner stringJoiner = new StringJoiner(" ");
		stringJoiner.add("JavaGoal");
		stringJoiner.add("is");
		stringJoiner.add("learning");
		stringJoiner.add("website");
		
		System.out.println("Are they equals: "+ str.equals(stringJoiner.toString()));
		System.out.println("Are they equals: "+ str.equals(stringJoiner));
	}

}

Output: Are they equals: true
Are they equals: false

2. StringJoiner add(CharSequence newElement): This method is used to add the given CharSequence in StringJoiner object. It adds the CharSequence value as the next element of the StringJoiner value. If the given element is null is adds the “null”.
It returns the object of StringJoiner after adding the value.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner stringJoiner = new StringJoiner(" ");
		stringJoiner.add("JavaGoal").add("learning").add("website").add(null);
		System.out.println(stringJoiner);
	}
}

Output: JavaGoal learning website null

3. StringJoiner merge(StringJoiner other): The method is used to add the two or more StringJoiner. It appends the specified StringJoiner at the end of the first StringJoiner. If the given StringJoiner is empty, the call has no effect. If first StringJoiner and second has different delimiter then elements from the second StringJoiner are concatenated with that delimiter.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner stringJoiner1 = new StringJoiner(",");
		stringJoiner1.add("JavaGoal");
		stringJoiner1.add("learning");
		stringJoiner1.add("website");
		
		StringJoiner stringJoiner2 = new StringJoiner(" ");
		stringJoiner2.add("A");
		stringJoiner2.add("B");
		stringJoiner2.add("C");
		
		System.out.println(stringJoiner1.merge(stringJoiner2));
	}
}

Output: JavaGoal,learning,website,A B C

4. StringJoiner length() : This method returns the length of the String that return by StringJoiner.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner stringJoiner = new StringJoiner(",");
		stringJoiner.add("JavaGoal");
		stringJoiner.add("learning");
		stringJoiner.add("website");
		
		System.out.println("Length: "+ stringJoiner.length());
	}
}

Output: Length: 25

5. StringJoiner setEmptyValue(CharSequence emptyValue): This method is used to sets the string representation when no one string is added in object.

import java.util.StringJoiner;

public class StringJoinerExample 
{
	public static void main(String[] args) 
	{
		StringJoiner stringJoiner = new StringJoiner(",");
		stringJoiner.setEmptyValue("No string added yet");
		System.out.println("StringJoiner object after creation:"+ stringJoiner);
		
		stringJoiner.add("JavaGoal");
		stringJoiner.add("learning");
		stringJoiner.add("website");
		System.out.println("StringJoiner object after added string:"+ stringJoiner);	
	}
}

Output: StringJoiner object after creation:No string added yet
StringJoiner object after added string:JavaGoal,learning,website

Leave a Comment