Java 8 StringJoiner

Introduction to Java 8 StringJoiner

In this short post, we will be covering Java 8 StringJoiner feature and will explore what are the different features and how this can be used more effectively with Java 8 Stream API.

 

Introduction 

Joining multiple strings in a very common tasks in day to day programming activity.There was no direct way to join multiple String in Java (Other than using the third party API’s). Java 8 Introduced a new class StringJoiner which can be used to join multiple Strings.StringJoiner is a kind of a Collector.

 

1. StringJoiner

StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.

 1.1 Using Delimiter

One of the common use cases of the StringJoiner is to use a delimiter to join strings.

StringJoiner joiner = new StringJoiner(",");
    joiner.add("Sunday");
    joiner.add("Monday");
    joiner.add("Tuesday");

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

Here is our Junit Test

@Test
public void stringJoinerTest(){

    StringJoiner joiner = new StringJoiner(",");
    joiner.add("Sunday");
    joiner.add("Monday");
    joiner.add("Tuesday");

    assertEquals("Sunday,Monday,Tuesday", joiner.toString());
}
 1.2 Using Delimiter, Prefix and Suffix

StringJoiner also provides the way to predefined prefix and suffix in addition to the delimiter.

public static void joinerWithDelimiterWithPrefixAndSuffix(){
    StringJoiner joiner = new StringJoiner(",", "Prefix-", "-Suffix");
    joiner.add("Sunday");
    joiner.toString();
}

Output 

Prefix-Sunday-Suffix

 

 1.3 Join Multiple StringJoiner

We can also merge multiple StringJoiner using StringJoiner’s merge() method

   StringJoiner joiner1= new StringJoiner("|");
   StringJoiner joiner2= new StringJoiner(";");
   StringJoiner joiner3= new StringJoiner(";");

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

   joiner3.add("three");
   joiner3.add("four");
   joiner1.merge(joiner3);

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

Output

one;two|three;four

 

2. String.join()

StringJoiner is used internally by two static String.join methods

String output= String.join("-", "2017","09","30");
2017-09-30 //output
List<String> list= Arrays.asList("one","two","three");
String output = String.join("-", list);

one-two-three //output

4. Collectors.joining

StringJoiner can be easily used with the new Java 8 Collection API.

List<Customer> customers = Arrays.asList(
        new Customer("John", "Smith"),
        new Customer("Umesh", "Awasthi"),
        new Customer("Andy", "Flower")
);

final String customerJoin = customers.stream()
        .map(Customer::getFirstName)
        .collect(Collectors.joining(","));

System.out.println(customerJoin);

Output

John, Umesh, Andy

 

5. Why StringJoiner

There can be a very natural question about why do we need StringJoiner when we already have StringBuilder? Or it internally use StringBuilder to perform most of the operations

With the Java 8 Stream API, StringJoiner is very useful as compared to the StringBuilder.Let’s take an example to understand the difference between using SpringJoiner and StringBuilder.

List<String> list = Arrays.asList("Foo","Bar");

//join string using StringJoiner
String output = list.stream().collect(Collectors.joining(","));

//using StringBuilder

String collectoutput =
        list.stream().collect(Collector.of(StringBuilder::new,
                (stringBuilder, str) -> stringBuilder.append(str).append(", "),
                StringBuilder::append,
                StringBuilder::toString));

 

Summary

In this post, we got an Introduction to Java8 StringJoiner. We explored various features of the StringJoiner class and how to use it to join multiple Strings.We can also think of StringJoiner as a kind of Collector and can be really useful when working with parallel streams.

JavaDevJournal

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

Leave a Reply

1 Comment on "Java 8 StringJoiner"

Notify of
avatar
Sort by:   newest | oldest | most voted
Travellingrants
Guest

Interesting article.

wpDiscuz