Blog Image

StringJoiner  

This blog i am explaining about the StringJoiner . StringJoiner is used to construct a sequence of characters separated by a delimiter and this class which methods are we are using with example i am explaining.

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.
Constructor:
1)StringJoiner(CharSequence delimiter)

  • delimiter we can pass a characters sequence , no prifix and suffix
2) StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
  • This constructor character sequences are delimiter and prefix ,suffix are mandatory fields and null should not be pass.
If passing the characters sequences(delimiter,prefix,suffix) as null it will throw java.lang.NullPointerException: The prefix must not be null see below example for understanding purpose,

public class SJGStringJoiner {

    public static void main(String[] args) {
       
        // String joiner method
         StringJoiner stringJoiner = new StringJoiner(null,null,null);
         String value = stringJoiner.toString();
         System.out.println(value);}


out put:
    Exception in thread "main" java.lang.NullPointerException: The prefix must not be null
    at java.util.Objects.requireNonNull(Unknown Source)
    at java.util.StringJoiner.<init>(Unknown Source)
    at com.sjg.stringjoiner.SJGStringJoiner.main(SJGStringJoiner.java:10)
 
Methods:
1)add(CharSequence newElement)
Adds a copy of the given CharSequence value as the next element of the StringJoiner value.
2)length():
Returns the length of the String representation of this StringJoiner.
3)merge(StringJoiner other):
Adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty.
4)setEmptyValue(CharSequence emptyValue):
this StringJoiner and no elements have been added yet, that is, when it is empty.
Note:Empty value should not be null,it will throws Exception
Exception in thread "main" java.lang.NullPointerException: The empty value must not be null

StringJoiner stringJoiner = new StringJoiner(",");
        stringJoiner.add("harish").add("manas").add("aditya").setEmptyValue(null).add("vasu");

output:
    at java.util.Objects.requireNonNull(Unknown Source)
    at java.util.StringJoiner.setEmptyValue(Unknown Source)
    at com.sjg.stringjoiner.SJGStringJoiner.main(SJGStringJoiner.java:14)

5)toString():
Returns the current value and we can Join a List by a delimiter:

See below example all the above methods are added,

public class SJGStringJoiner {
    public static void main(String[] args) {
        String finalValue = null;
        /*** using string joiner methods add,setEmpty() ,length() and toString() ***/

        StringJoiner stringJoiner = new StringJoiner(",");
        stringJoiner.add("srinu").add("vasu").add("shiva").setEmptyValue("").add("ganesh");
        String value = stringJoiner.toString();
        int valueLength = stringJoiner.toString().length();
        System.out.println(value);
        System.out.println(valueLength);

        /*** merging the string joiners ***/

        StringJoiner stringJoiner1 = new StringJoiner(":", "[", "]");
        stringJoiner1.add("welcome to SJG group").merge(stringJoiner);
        String mergeValue = stringJoiner1.toString();
        System.out.println(mergeValue);

        /** passing list values by using delimeter **/

        // joining the list by using delimeter
        List<String> list = Arrays.asList("java", "spring", "web services", "J2ee");
        String result = String.join("-", list);
        System.out.println(result);

        // another way to joining the list with prifix and sufix
        StringJoiner joiner = new StringJoiner(",", "[", "]");
        for (String element : list) {
            joiner.add(element);
            finalValue = joiner.toString();
        }
        System.out.println(finalValue);
    }

}



output:
srinu,vasu,shiva,ganesh
23
[welcome to SJG group:srinu,vasu,shiva,ganesh]
java-spring-web services-J2ee
[java,spring,web services,J2ee]

You can download the above maven example from the attachments given below

About author

User Image
sagarreddy

Seeking a challenging position, where I will be a valuable team member, contributing quality ideas and work for an organization where there is an ample scope to grow for an individual as well as organization growth in Software Development.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.