Using StringJoiner in Java

Using StringJoiner in Java

What is StringJoiner?

The StringJoiner class in Java 8 is one of the new classes that we can find in this version of Java. Many new features were included in Java 8 and one of them could be this new class. What is StringJoiner for? It is used to merge a set of strings with a delimiter in a very simple way.

An example of how to use StringJoiner would be the following:

package dev.juanc4milo;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 *
 * @author juanc4milo
 */
public class TestStringJoiner {

    public static void main(String[] args) { 
    List<String> firstSetWords=new ArrayList<>();

    firstSetWords.add("this");
    firstSetWords.add("is");
    firstSetWords.add("a");
    firstSetWords.add("test");
    firstSetWords.add("with");
    firstSetWords.add("stringjoiner");
    firstSetWords.add("in");
    firstSetWords.add("java8");

    StringJoiner groupingFirstSetOfWords= new StringJoiner(",");

    firstSetWords.forEach((string) -> {
        groupingFirstSetOfWords.add(string);
        });

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

In output console you would see:

Output:
this,is,a,test,with,stringjoiner,in,java8

StringJoiner - String delimited by prefix and sufix

In the above example, a string set was grouped as a single string separated by a delimiter (comma). With StringJoiner it can also be initialized by defining a prefix and a suffix. That is one character at the beginning of the string and another character at the end of the string. To do this, in the constructor class you must say what the prefix and suffix will be.

Let's see this in the following lines of code:

StringJoiner groupingFirstSetOfWords= new StringJoiner(",","[","]");

    firstSetWords.forEach((string) -> {
        groupingFirstSetOfWords.add(string);
        });

    System.out.println(groupingFirstSetOfWords.toString());

In the output console would see:

Output:
[this,is,a,test,with,stringjoiner,in,java8]

Merging StringJoiners

You can also concatenate two StringJoiner to form a large single string set of text using the merge method.

Keep in mind that if you use delimiters, the delimiter of whoever performs the merge function prevails:

package dev.juanc4milo;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 *
 * @author juanc4milo
 */
public class TestStringJoiner {

    public static void main(String[] args) { 
    List<String> firstSetWords=new ArrayList<>();

    firstSetWords.add("this");
    firstSetWords.add("is");
    firstSetWords.add("a");
    firstSetWords.add("test");
    firstSetWords.add("with");
    firstSetWords.add("stringjoiner");
    firstSetWords.add("in");
    firstSetWords.add("java8");

    StringJoiner groupingFirstSetOfWords= new StringJoiner(",","[","]");

    firstSetWords.forEach((string) -> {
        groupingFirstSetOfWords.add(string);
        });

    List<String> lastSetWords=new ArrayList<>();

    lastSetWords.add("Enjoy");
    lastSetWords.add("this");
    lastSetWords.add("blog post");

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

    lastSetWords.forEach((string) -> {
        groupingLastSetOfWords.add(string);
        });

    groupingLastSetOfWords.merge(groupingFirstSetOfWords);

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

In the output console would see:

Output:
{Enjoy,this,blog post,this,is,a,test,with,stringjoiner,in,java8}

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter.