Joiner class | Guava | Java
Last Updated :
15 Nov, 2018
Guava’s Joiner class provides various methods to handle joining operations on string, objects, etc. This class provides advanced functionality for the join operation.
Declaration: Following is the declaration for com.google.common.base.Joiner class :
@GwtCompatible
public class Joiner
extends Object
The following table gives a brief summary of the methods provided by Guava’s Joiner class:
Example:
import com.google.common.base.Joiner;
import java.util.*;
class GFG {
public static void main(String[] args)
{
String[] arr = { "one" , "two" , "three" , "four" };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
String result = Joiner.on( "..." )
.join(arr);
System.out.println( "Joined String: "
+ result);
}
}
|
Output:
Original Array: [one, two, three, four]
Joined String: one...two...three...four
Some more methods provided by Guava’s Joiner class are:
Example:
import com.google.common.base.Joiner;
import java.util.*;
class GFG {
public static void main(String[] args)
{
String[] arr = { "one" , "two" , null ,
"four" , null , "five" };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
String result = Joiner.on( '+' )
.skipNulls()
.join(arr);
System.out.println( "Joined String: "
+ result);
}
}
|
Output:
Original Array: [one, two, null, four, null, five]
Joined String: one+two+four+five
Reference: Google Guava Joiner Class
Share your thoughts in the comments
Please Login to comment...