Explain about StringJoiner in java8?


Since Java8 StringJoiner class is introduced this you can construct a sequence of characters separated by desired delimiter.

Sr.NoConstructor & Description
1StringJoiner(CharSequence delimiter)
This constructor creates an empty (no characters) StringJoiner, just with a copy of the specified delimiter.
2StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
This constructs a StringJoiner without characters.

Methods of StringJoiner class

add() − This method accepts a CharacterSequence object (Segment, String, StringBuffer, StringBuilder) and adds it to the current Joiner separating the next and previous elements (if any) with delimiter at the time of constructing it.

Example

import java.util.StringJoiner;
public class StringJoinerExample {
   public static void main(String args[]) {
      StringJoiner joiner = new StringJoiner(" ");
      joiner.add("Hello");
      joiner.add("how");
      joiner.add("are");
      joiner.add("you");
      System.out.println(joiner);
   }
}

Output

Hello how are you

length() − This method returns the length of the current Joiner i.e. number characters in it including the delimiter.

Example

import java.util.StringJoiner;
public class StringJoinerExample {
   public static void main(String args[]) {
      StringJoiner joiner = new StringJoiner(" ");
      joiner.add("Hello");
      joiner.add("how");
      joiner.add("are");
      joiner.add("you");
      System.out.println("Contents: "+joiner);
      System.out.println("length: "+joiner.length());
   }
}

Output

Contents: Hello how are you
length: 17

merge() − This method accepts a StringJoiner object, adds/concatenates it to the current one and returns the resultant StringJoiner object.

Example

import java.util.StringJoiner;
public class StringJoinerExample {
   public static void main(String args[]) {
      StringJoiner joiner1 = new StringJoiner(" ");
      joiner1.add("Hello");
      joiner1.add("how");
      joiner1.add("are");
      joiner1.add("you");
      StringJoiner joiner2 = new StringJoiner(" ");
      joiner2.add("Welcome");
      joiner2.add("to");
      joiner2.add("Tutorialspoint");
      StringJoiner result = joiner1.merge(joiner2);
      System.out.println("Contents: "+result);
   }
}

Output

Contents: Hello how are you Welcome to Tutorialspoint

setEmptyValue() − This method accepts an CharacterSequence object as a parameter and sets it as a value of empty Sting for the current StringJoiner object i.e. if no elements are added to a StringJoiner object if you print its contents the value you have set using this method will be displayed. In short, once you invoke this method on a StringJoiner object it is never considered empty.

Example

import java.util.StringJoiner;
public class StringJoinerExample {
   public static void main(String args[]) {
      StringJoiner joiner = new StringJoiner(" ");
      joiner.setEmptyValue("sample empty value");
      System.out.println(joiner);
   }
}

Output

sample empty value

toString() − This method returns the contents of the current StringJoiner as a Sting object.

import java.util.StringJoiner;
public class StringJoinerExample {
   public static void main(String args[]) {
      StringJoiner joiner = new StringJoiner(" ");
      joiner.add("Hello");
      joiner.add("how");
      joiner.add("are");
      joiner.add("you");
      String value = joiner.toString();
      System.out.println(value);
   }
}

Output

Hello how are you

Updated on: 10-Oct-2019

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements