Java StringJoiner Class Examples
Java 8 introduced some new features e.g default and static method and lambda expression, parallel array sort, method reference, etc. But here we are going to learn in detail which is java 8 StringJoiner class.
Java StringJoiner
StringJoiner is a new feature of java 8 technology and this class is belongs to the java.util package.
Java StringJoiner is public final class and extend Object class in java.
Syntax: public final StringJoiner extends Object
StringJoiner class is used to construct a sequence of characters separated by delimiters i.e you can create string by using some delimiters like ","(comma) and "-"(hyphen), etc. We can also use prefix and suffix for character sequence.
StringJoiner class is used to construct a sequence of characters separated by delimiters i.e you can create string by using some delimiters like ","(comma) and "-"(hyphen), etc. We can also use prefix and suffix for character sequence.
Java StringJoiner Constructor
There are two constructor of StringJoiner class provided by java.
- StringJoiner(CharSequence delimiter).
- StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix).
Java StringJoiner Methods
There are some StringJoiner methods which we will use in string joiner examples.
- StringJoiner add(CharSequence newElement).
- int length().
- StringJoiner merge(StringJoiner other).
- StringJoiner setEmptyValue(CharSequence emptyValue).
- String toString().
Java StringJoiner Example by using delimiters
This is the first example of StringJoiner where we use delimiter for adding some strings and using add() method of StringJoiner class.
import java.util.*;
class Demo
{
public static void main(String args[])
{
StringJoiner sj = new StringJoiner(",");//passing comma (,) as delimiter
StringJoiner sj1 = new StringJoiner("-");//passing hyphen (-) as delimiter
//Adding some string with comma(,) delimiter
sj.add("suresh");
sj.add("gurpreet");
sj.add("piyush");
sj.add("jagdish");
System.out.println(sj);
System.out.println();
System.out.println();
//Adding some string with hyphen(-) delimiter
sj1.add("simran");
sj1.add("siya");
sj1.add("khusboo");
sj1.add("barkha");
System.out.println(sj1);
}
}
Output: suresh,gurpreet,piyush,jagdish
simran-siya-khusboo-barkha
Output: suresh,gurpreet,piyush,jagdish
simran-siya-khusboo-barkha
Java StringJoiner Example by using prefix and suffix
import java.util.StringJoiner;
class Demo1
{
public static void main(String args[])
{
//passing , as delimiter and using "(" opening bracket as prefix and ")" closing bracket as suffix.
StringJoiner sj = new StringJoiner(",","(",")");
sj.add("blue");
sj.add("red");
sj.add("pink");
sj.add("white");
System.out.println(sj);
}
}
Output: (blue,red,pink,white)
Output: (blue,red,pink,white)
Java StringJoiner Example: Merge two StringJoiner
In this example, We will take 2 StringJoiner and then use merge() method of StringJoiner class for merging or adding 2 StringJoiner.
import java.util.*;
class Demo3
{
public static void main(String args[])
{
//Creating first StringJoiner with delimiter and prefix and suffix
StringJoiner sj1 = new StringJoiner(",","*","*");
sj1.add("rose");
sj1.add("lotus");
System.out.println(sj1);
//Creating second StringJoiner with delimiter and prefix and suffix
StringJoiner sj2 = new StringJoiner(":","*","*");
sj2.add("red");
sj2.add("pink");
System.out.println(sj2);
//Now using merge() method for merging sj1 and sj2 joiners
StringJoiner mrg = sj1.merge(sj2);
System.out.println(mrg);
}
}
Output: *rose,lotus*
*red:pink*
*rose,lous,red:pink*
import java.util.*;
class Demo4
{
public static void main(String args[])
{
StringJoiner sj1 = new StringJoiner(",");
/* if StringJoiner is empty then we can print a massage for this StringJoiner by using setEmptyValue(). */
sj1.setEmptyValue("This is empty StringJoiner");
System.out.println(sj1);
System.out.println("add some values");
//now adding some string by using add() method
sj1.add("c++");
sj1.add("html");
System.out.println("added values is: "+sj1);
//After adding values, let's count the length of string by using length() method
int length = sj1.length();
System.out.println("Length of SJ "+length);
}
}
Output: This is empty StringJoiner
add some values
added values is: c++,html
Length of SJ 8
I hope, this java 8 StringJoiner class example article will be useful to you.
import java.util.*;
class Demo3
{
public static void main(String args[])
{
//Creating first StringJoiner with delimiter and prefix and suffix
StringJoiner sj1 = new StringJoiner(",","*","*");
sj1.add("rose");
sj1.add("lotus");
System.out.println(sj1);
//Creating second StringJoiner with delimiter and prefix and suffix
StringJoiner sj2 = new StringJoiner(":","*","*");
sj2.add("red");
sj2.add("pink");
System.out.println(sj2);
//Now using merge() method for merging sj1 and sj2 joiners
StringJoiner mrg = sj1.merge(sj2);
System.out.println(mrg);
}
}
Output: *rose,lotus*
*red:pink*
*rose,lous,red:pink*
Java StringJoiner Methods Example
Here we will use some methods of StringJoiner class.import java.util.*;
class Demo4
{
public static void main(String args[])
{
StringJoiner sj1 = new StringJoiner(",");
/* if StringJoiner is empty then we can print a massage for this StringJoiner by using setEmptyValue(). */
sj1.setEmptyValue("This is empty StringJoiner");
System.out.println(sj1);
System.out.println("add some values");
//now adding some string by using add() method
sj1.add("c++");
sj1.add("html");
System.out.println("added values is: "+sj1);
//After adding values, let's count the length of string by using length() method
int length = sj1.length();
System.out.println("Length of SJ "+length);
}
}
Output: This is empty StringJoiner
add some values
added values is: c++,html
Length of SJ 8
I hope, this java 8 StringJoiner class example article will be useful to you.
nice post about java 8 features.
ReplyDelete