Blog Image


This blog i am explaining about the StringJoiner . StringJoiner is used to construct a sequence of characters separated by a delimiter and this class which methods are we are using with example i am explaining.

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
1)StringJoiner(CharSequence delimiter)

  • delimiter we can pass a characters sequence , no prifix and suffix
2) StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
  • This constructor character sequences are delimiter and prefix ,suffix are mandatory fields and null should not be pass.
If passing the characters sequences(delimiter,prefix,suffix) as null it will throw java.lang.NullPointerException: The prefix must not be null see below example for understanding purpose,

public class SJGStringJoiner {

    public static void main(String[] args) {
        // String joiner method
         StringJoiner stringJoiner = new StringJoiner(null,null,null);
         String value = stringJoiner.toString();

out put:
    Exception in thread "main" java.lang.NullPointerException: The prefix must not be null
    at java.util.Objects.requireNonNull(Unknown Source)
    at java.util.StringJoiner.<init>(Unknown Source)
    at com.sjg.stringjoiner.SJGStringJoiner.main(
1)add(CharSequence newElement)
Adds a copy of the given CharSequence value as the next element of the StringJoiner value.
Returns the length of the String representation of this StringJoiner.
3)merge(StringJoiner other):
Adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty.
4)setEmptyValue(CharSequence emptyValue):
this StringJoiner and no elements have been added yet, that is, when it is empty.
Note:Empty value should not be null,it will throws Exception
Exception in thread "main" java.lang.NullPointerException: The empty value must not be null

StringJoiner stringJoiner = new StringJoiner(",");

    at java.util.Objects.requireNonNull(Unknown Source)
    at java.util.StringJoiner.setEmptyValue(Unknown Source)
    at com.sjg.stringjoiner.SJGStringJoiner.main(

Returns the current value and we can Join a List by a delimiter:

See below example all the above methods are added,

public class SJGStringJoiner {
    public static void main(String[] args) {
        String finalValue = null;
        /*** using string joiner methods add,setEmpty() ,length() and toString() ***/

        StringJoiner stringJoiner = new StringJoiner(",");
        String value = stringJoiner.toString();
        int valueLength = stringJoiner.toString().length();

        /*** merging the string joiners ***/

        StringJoiner stringJoiner1 = new StringJoiner(":", "[", "]");
        stringJoiner1.add("welcome to SJG group").merge(stringJoiner);
        String mergeValue = stringJoiner1.toString();

        /** passing list values by using delimeter **/

        // joining the list by using delimeter
        List<String> list = Arrays.asList("java", "spring", "web services", "J2ee");
        String result = String.join("-", list);

        // another way to joining the list with prifix and sufix
        StringJoiner joiner = new StringJoiner(",", "[", "]");
        for (String element : list) {
            finalValue = joiner.toString();


[welcome to SJG group:srinu,vasu,shiva,ganesh]
java-spring-web services-J2ee
[java,spring,web services,J2ee]

You can download the above maven example from the attachments given below

About author

User Image

my self is SagarReddy. I from bhadrachalam and i am working StackSpace software solutions pvt. Ltd. has a java developer. Learning the new things from authors to update my self to build my career Better Way.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.