Saturday, 25 October 2014

Joining Strings With Delimiter Using StringJoiner Class In Java 8

     In any programming languages, and in many applications, developers have to put list of values. Frequently these are comma ( “,” ) seperated values, but they can also incorporate other string separators. Java 8 added a new class StringJoiner which is used for this purpose.

String Joiner class is added in Java8 under util package. Using String Joiner class, you can join any number of strings with a specified delimiter and starting with a supplied prefix string and ending with a supplied suffix.

The StringJoiner class has two parameterized constructors.
1. StringJoiner(CharSequence delimiter)
2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

Let’s see a simple example, where Strings joining is required.
If you run the below code, you will get the output as [java 6, java 7, java 8, java 9] . Here the delimiter is ",", and prefix is "[", and suffix is "]" .
package com.speakingcs.practice;
import java.util.ArrayList;
import java.util.List;


public class TestMain {


    public static void main(String[] args) {

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

        aList.add("java 6");

        aList.add("java 7");

        aList.add("java 8");

        aList.add("java 9");

        String str = aList.toString();

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

    }

}

Using String Joiner Class :

Using StringJoiner class, we can get the same output with the given code.
package com.speakingcs.practice;
import java.util.StringJoiner;


public class StringJoinerDemo {
    

    public static void main(String[] args) {        

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

        sj.add("java 6").add("java 7").add("java 8").add("java 9");

        System.out.println(sj);        

    }

}

if you don’t want the result with prefix and suffix strings, use the constructor with only one argument.
package com.speakingcs.practice;
import java.util.StringJoiner;


public class StringJoinerDemo {
    

    public static void main(String[] args) {        

        StringJoiner sj = new StringJoiner(",");

        sj.add("java 6").add("java 7").add("java 8").add("java 9");

        System.out.println(sj);        

    }

}

The output of the above program is java 6, java 7, java 8, java 9.

Finding The Length of the Result String :

The StringJoiner class has a method length() which returns the number of characters in the String representation of StringJoiner class.

Merging Two StringJoiner Instances :

The merge() method of StringJoiner class, can be used to add contents of a given two StringJoiners. The method declaration is

StringJoiner merge(StringJoiner another);

Eg:
StringJoiner sj2 = new StringJoiner(":");

sj2.add("Java 10").add("java 11").add("java 12").add("java 13");

sj.merge(sj2);

System.out.println(sj);

In the above sample code, we have created another StirngJoiner sj2 and we called merge method on sj. The output for the above program is
java 6,java 7,java 8,java 9,Java 10:java 11:java 12:java 13

Setting Empty Value :

In case of no Strings are added to StringJoiner class, the toString() method will return prefix + suffix. If you want to return any specified string in such cases, call the setEmptyValue method by passing a String value as argument.
StringJoiner sj = new StringJoiner(",","[","]");

sj.setEmptyValue("No values");

System.out.println(sj);

Getting Result :

You can get the result String by calling toString() method on the instance of StringJoiner class. The toString() method returns a string which consists of the prefix, Values sperated by delimiter, and the suffix.

Incase of no values and no empty value, it returns prefix + suffix. Incase of no values and if empty value is set, it returns empty value.

Working With Collections :

If you have a collections of Strings and want to join them with StringJoiner, you can do that as mentioned below.

 A simple and iterative approach is
StringJoiner sj = new StirngJoiner(“,”);

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

aList.add("java 7"); aList.add("java 8"); aList.add("java 9");

for(String aValue : aList) {

    sj.add(aValue);

}

System.out.println(sj);

Using Lambda Expressions
aList.forEach(aValue -> sj.add(aValue));

System.out.println(sj);

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Note: only a member of this blog may post a comment.