StringJoiner class in java (JDK 1.8 Version Concept)
Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence.
Constructors :
StringJoiner(CharSequence delimiter) : Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
Syntax :
public StringJoiner(CharSequence delimiter)
Parameters :
delimiter - the sequence of characters to be used between
each element added to the StringJoiner value
Throws:
NullPointerException - if delimiter is null
Syntax :
public StringJoiner(CharSequence delimiter,
CharSequence prefix, CharSequence suffix)
Parameters :
delimiter - the sequence of characters to be used between
each element added to the StringJoiner value
prefix - the sequence of characters to be used at the beginning
suffix - the sequence of characters to be used at the end
Throws:
NullPointerException - if prefix, delimiter, or suffix is null
Methods : There are 5 methods in StringJoiner class.
String toString() : This method returns String object of this StringJoiner.
Syntax :
public String toString()
Parameters : NA
Returns :the string representation of this StringJoiner
Overrides :
toString in class Object
StringJoiner add(CharSequence newElement) : This method adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then “null” is added.
Syntax :
public StringJoiner add(CharSequence newElement)
Parameters :
newElement - The element to add
Returns :
a reference to this StringJoiner
StringJoiner merge(StringJoiner other) : This method adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect.
If the other StringJoiner is using a different delimiter, then elements from the other StringJoiner are concatenated with that delimiter and the result is appended to this StringJoiner as a single element.
Syntax :
public StringJoiner merge(StringJoiner other)
Parameters :
other - The StringJoiner whose contents should be merged
into this one
Returns :
This StringJoiner
Throws :
NullPointerException - if the other StringJoiner is null
int length() : This method returns the length of the String representation of this StringJoiner.
Syntax :
public int length()
Parameters :
NA
Returns :
This StringJoiner
StringJoiner setEmptyValue(CharSequence emptyValue) : This method sets string to be used when determining the string representation of this StringJoiner and no elements have been added yet, that is, when it is empty.
Syntax :
public StringJoiner setEmptyValue(CharSequence emptyValue)
Parameters :
emptyValue - the characters to return as the value
of an empty StringJoiner
Returns :
this StringJoiner itself so the calls may be chained
Throws:
NullPointerException - when the emptyValue parameter is null
//Wap to demonstrate the concept of StringJoiner Class in java 8 ?
package java8concept;
import java.util.StringJoiner;
public class StringJoinerDemo {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner(",");
//StringJoiner sj = new StringJoiner("_");
StringJoiner sjnew = new StringJoiner(",", "[", "]"); // passing comma(,) and square-brackets as delimiter
sj.add("Java");
sj.add("Technology");
sj.add("Govind");
sj.add("Sir");
System.out.println(sj);
sjnew.add("Java");
sjnew.add("Technology");
sjnew.add("Govind");
sjnew.add("Sir");
System.out.println(sjnew);
}
}
o/p:-----------
Java,Technology,Govind,Sir
[Java,Technology,Govind,Sir]
========================================================================
Wap to merge two StringJoiner object.
package java8concept;
import java.util.StringJoiner;
public class StringJoinerMergedemo {
public static void main(String[] args) {
StringJoiner sj = new StringJoiner(",", "[", "]");
sj.add("Govind");
sj.add("Sir");
StringJoiner sjnew = new StringJoiner(":", "[", "]");
sjnew.add("Java");
sjnew.add("Technology");
StringJoiner merge= sj.merge(sjnew);
System.out.println(merge);
}
}
o/p--------------
[Govind,Sir,Java:Technology]
============================================================================
//wap to demonstrate the method of StringJoiner class?
package java8concept;
import java.util.StringJoiner;
public class StringJoinerMethodDemo {
public static void main(String[] args) {
StringJoiner sj=new StringJoiner(",");
System.out.println(sj);
sj.setEmptyValue("It Is empty");
System.out.println(sj);
sj.add("Java");
sj.add("Technology");
System.out.println(sj);
// Returns length of StringJoiner
int length = sj.length();
System.out.println(length);
// Returns StringJoiner as String type
String stringdata = sj.toString();
System.out.println(stringdata);
// Now, we can apply String methods on it
char chardata = stringdata.charAt(3);
System.out.println(chardata);
sj.add("Govind Sir");
System.out.println(sj);
int newleggth = sj.length();
System.out.println(newleggth);
}
}
o/p:----------------
Java,Technology
15
Java,Technology
a
Java,Technology,Govind Sir
26