DEV Community

Cover image for Using StringJoiner in Java
juanc4milo
juanc4milo

Posted on • Updated on • Originally published at juanc4milo.dev

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());
  }
}
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

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());
  }
}
Enter fullscreen mode Exit fullscreen mode

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 follow me and check out my webpage to subscribe to my email newsletter.

Other articles that you might like...

Do you like it? You can buy me a beer if you want.

Originally published at juanc4milo.dev

Discussion (2)

Collapse
sumit profile image
Sumit Singh

Thanks.. my doubt are pretty much clear..
Just a suggestion, when you are giving examples of code snippets..just write down the name of the language you are using just after the the tripple backticks (`). It will make code looks much better.

Collapse
juanc4milo profile image
juanc4milo Author

Thanks a lot for your feedback.