StringJoiner in Java 8

In this article let us discuss about the new class – StringJoiner which has been introduced in Java 8.

What is StringJoiner?

StringJoiner is a class which is used to construct sequence of characters separated by delimiter and with optional prefix and suffix.

Ways to create StringJoiner:

There are 2 ways we can initialize a StringJoiner,

  • StringJoiner(CharSequence delimiter)
  • StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

Now let us see an example,

public class StringExample1 {

    public static void main(String args[]) {

        StringJoiner joiner = new StringJoiner("||");
        joiner.add("Alpha");
        joiner.add("Beta");
        joiner.add("Charlie Delta");
        System.out.println(joiner);

    }
}

We have created a StringJoiner with delimiter – “||” and have added some strings.

The output will be,

Output

What will happen if we give the delimiter as null?

public class StringExample1 {

    public static void main(String args[]) {

        StringJoiner joiner = new StringJoiner(null); //Will this work?
        joiner.add("Alpha");
        joiner.add("Beta");
        joiner.add("Charlie Delta");
        System.out.println(joiner);

    }
}

The above code will throw null pointer exception.

Exception

As per the internal implementation of StringJoiner, it will check with Objects not null to validate if the delimiter is present.

Objects.requireNonNull(delimiter, "The delimiter must not be null");

StringJoiner with Prefix and Suffix:

Now let us create a StringJoiner with prefix and suffix.

public class StringExample1 {

    public static void main(String args[]) {

        StringJoiner joiner = new StringJoiner("||", "[", "]");
        joiner.add("Alpha");
        joiner.add("Beta");
        joiner.add("Charlie Delta");
        System.out.println(joiner);

    }
}

Here we have included the delimiter as “||” and with prefix “[” and suffix “]”.

output

What are the other methods available?

There are some inbuild methods available for StringJoiner,

  • add
  • length
  • merge
  • setEmptyValue

We have already seen about add in the above example.

Length:

Now let us see Length,

It returns the length of the StringJoiner.

public class StringExample1 {

    public static void main(String args[]) {

        //Scenario 1
        StringJoiner joiner = new StringJoiner("||", "[", "]");
        joiner.add("Alpha");
        joiner.add("Beta");
        joiner.add("Charlie Delta");
        System.out.println(joiner.length());

        //Scenario 2
        StringJoiner joiner1 = new StringJoiner("||", "[","]" );
        System.out.println(joiner1.length());

        //Scenario 3
        StringJoiner joiner2 = new StringJoiner("||");
        System.out.println(joiner2.length());


    }
}

From the above code we have triggered 3 scenario’s,

Scenario 1:

The length of the string will be calculated along with delimiter, prefix and suffix

Scenario 2:

What if there are no strings added to the StringJoiner?

In this case the length will be calculated with prefix + suffix.

Scenario 3:

Empty StringJoiner with only delimiter. In this scenario the length will be empty.

Merge:

We can merge 2 String joiners to combine it for one output.

public class StringExample1 {

    public static void main(String args[]) {

        StringJoiner joiner = new StringJoiner("||", "[", "]");
        joiner.add("Alpha");
        joiner.add("Beta");
        joiner.add("Charlie Delta");

        StringJoiner joiner1 = new StringJoiner("::", "[","]" );
        joiner1.add("Gamma");
        joiner1.add("Hamilton");
        joiner.merge(joiner1);
        System.out.println(joiner);

    }
}

From the above code we have merged joiner with joiner 1, let us see the output.

output

setEmptyValue:

Sets the sequence of characters to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty. A copy of the emptyValue parameter is made for this purpose.

How does this work?

public class StringExample1 {

    public static void main(String args[]) {

        StringJoiner joiner = new StringJoiner(",").setEmptyValue("Empty");

        System.out.println(joiner);

        joiner.add("New Value Added");

        System.out.println(joiner);

    }
}

It is like adding a default value for the StringJoiner, when we execute the above code – First we will get “Empty” but once the value is added to the StringJoiner then the value will be “New Value Added”.

By Sri

Leave a Reply

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