Using StringJoiner in Java
Tags: java, string
Published: 2020-11-14 14:03:51

## 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:


```java
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: <br>
> 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:


```java
StringJoiner groupingFirstSetOfWords= new StringJoiner(",","[","]");
    
    firstSetWords.forEach((string) -> {
        groupingFirstSetOfWords.add(string);
        });
    
    System.out.println(groupingFirstSetOfWords.toString());
``` 

In the output console would see:

> Output: <br>
> [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:


```java
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: <br>
> {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](https://juanc4milo.dev) to subscribe to my email newsletter.

### Other articles that you might like...

* [Redis - Instalación y Configuración](https://juanc4milo.dev/redis-instalacion-y-configuracion)
* [AWS S3 Batch Operations](https://juanc4milo.dev/aws-s3-batch-operations)
* [AWS S3 Inventory](https://juanc4milo.dev/inventario-aws-s3)
* [Qué ofrece y cómo se configura AWS Elasticache?](https://juanc4milo.dev/aws-elasticache)
* [Qué ofrece y cómo se configura Elasticache en AWS?](https://juanc4milo.dev/aws-elasticache)
*  [How to use Redis with Spring in Java](https://juanc4milo.dev/redis-with-spring-in-java)
* [How to send an email with AWS SES and track events with Configuration Sets?](https://juanc4milo.dev/send-email-with-aws-and-track-events)
* [How to receive emails in a S3 bucket with AWS SES?](https://juanc4milo.dev/receive-emails-in-s3-with-aws-ses)
* [How to add a Sold Out snippet or badge on a particular product using Woocommerce?](https://juanc4milo.dev/add-sold-out-badge-product-using-woocommerce)

**Do you like it?** You can [buy me a beer](https://buymeacoffee.com/juanc4milo) if you want.

> _Originally published at [juanc4milo.dev](https://juanc4milo.dev)_