Java 8 Tutorial : StringJoiner example

StringJoiner class in Java8 explaination with Example :

‘StringJoiner’ class was introduced in Java8 . Using this class, we can join a number of different strings easily. Two different constructs are available for ‘StringJoiner’ to instantiate it.

StringJoiner(CharSequence delimiter) :

It constructs a StringJoiner with the separator as ‘delimiter’.

StringJoiner(CharSequence delimiter, CharSequence p, CharSequence s) :

It will construct a StringJoiner with separator as ‘delimiter’ , prefix ‘p’ and suffix ‘s’ for the final output.

To add an element to StringJoiner, ‘add(CharSequence newElement)’ method is used .’newElement’ will be added as the next element to the StringJoiner .

Example of joining string with comma as delimiter using StringJoiner in Java8 :

import java.util.StringJoiner;

/**
 * Example class of StringJoiner 
 */
public class Test {

    public static void main(String[] args) {
        
        //create a StringJoiner object with delimiter comma
        StringJoiner joiner = new StringJoiner(",");
        
        //add elements to the StringJoiner Object
        joiner.add("Monday");
        joiner.add("TuesDay");
        joiner.add("WednesDay");
        joiner.add("ThrusDay");
        joiner.add("Friday");
        
        //print out the result
        System.out.println(joiner.toString());
    }
}

It will print the following output :

Monday,TuesDay,WednesDay,ThrusDay,Friday

As you can see above that we have created one StringJoiner object first with comma as delimeter. Added elements to it using add() method and it automatically added the comma as a separator between them.

Example of StringJoiner with prefix and suffix :

Let’s try to create the string {one,two,three,four} using StringJoiner. The example will be same as above , only difference is that we will add ‘{‘ as prefix and ‘}’ as suffix for the final result string.Prefix and suffix can be added using a different constructor of StringJoiner.

import java.util.StringJoiner;

/**
 * Example class of StringJoiner
 */
public class Test {

    public static void main(String[] args) {

        //create a StringJoiner object with delimiter comma, prefix as { and suffix as }
        StringJoiner joiner = new StringJoiner(",","{","}");

        //add elements to the StringJoiner Object
        joiner.add("one");
        joiner.add("two");
        joiner.add("three");
        joiner.add("four");

        //print out the result
        System.out.println(joiner.toString());
    }
}

Merging Two StringJoiner :

We can merge two StringJoiner using merge(StringJoiner other) method. The prefix and suffix of ‘other’ StringJoiner will be ignored. It will only add the elements to the first StringJoiner. Output is also a StringJoiner. Example :

import java.util.StringJoiner;

/**
 * Example class of StringJoiner
 */
public class Test {

    public static void main(String[] args) {

        StringJoiner joiner1 = new StringJoiner(",","{","}");

        joiner1.add("one");
        joiner1.add("two");


        StringJoiner joiner2 = new StringJoiner(":","[","]");

        joiner2.add("three");
        joiner2.add("four");

        //print out the result
        System.out.println(joiner1.merge(joiner2).toString());
    }
}

Output :

{one,two,three:four}

Instead of writing one ‘add’ statement on different lines, we can also construct the whole object in one line like below :

import java.util.StringJoiner;

/**
 * Example class of StringJoiner
 */
public class Test {

    public static void main(String[] args) {

        //print out the result
        System.out.println(new StringJoiner(",","{","}").add("one").add("two").add("three").add("four").toString());
    }
}

Output :

{one,two,three,four}

Set an empty value to a StringBuilder :

We can set one message to display if no elements have been added to the StringBuilder, i.e. when it is empty. ‘setEmptyValue(CharSequence emptyValue)’ is used for this purpose. But, if any value is added even if it is an empty string, the StringJoiner will no longer considered as empty.

import java.util.StringJoiner;

/**
 * Example class of StringJoiner
 */
public class Test {

    public static void main(String[] args) {

        //print out the result
        StringJoiner joiner = new StringJoiner(",");
        joiner.setEmptyValue("No values are added.");

        System.out.println(joiner.toString());
    }
}

It will print No values are added. as output.

Sitewide-USD 336x280

Leave a Reply