Joiner class
is used to concatenate strings together with some delimiter. You may think
that, what extra it is offering to you. Following are some of the things that
you can get.
a.
You
can concatenate multiple strings using delimeter.
b.
You
can concatenate data in any collection like List, Set, Map and any other which
implements Iterable interface.
c.
You
can write final result to StringBuilder
d.
You
can write final concatenated result to a file
e.
You
can write final concatenated result to any any Appendable interface
implementations like BufferedWriter, CharArrayWriter, CharBuffer, FileWriter,
FilterWriter, LogStream, OutputStreamWriter, PipedWriter, PrintStream,
PrintWriter, StringBuffer, StringBuilder, StringWriter, Writer.
f.
You
can handle null values while concatenating.
Lets start
with simple example,
import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner = Joiner.on("$"); String result = joiner.join("HariKrishna", "Karnataka", "Bangalore", "Marthalli"); System.out.println(result); } }
Output
HariKrishna$Karnataka$Bangalore$Marthalli
Joiner.on("$")
Above
statement returns Joiner instance, which concatenates strings using the
delimiter "$".
joiner.join("HariKrishna", "Karnataka",
"Bangalore", "Marthalli")
Joins
strings using the separator ($: It specified at the time of Joiner instance
initialization).
While
joining strings, you must be aware of null values. You must handle null values,
otherwise java.lang.NullPointerException thrown. It is because, while joining
elements, toString method of the elements called, If element is null, obviously
it throws java.lang.NullPointerException.
import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner = Joiner.on("$"); String result = joiner.join("HariKrishna", "Karnataka", "Bangalore", null); System.out.println(result); } }
Output
Exception in thread "main" java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210) at com.google.common.base.Joiner.toString(Joiner.java:433) at com.google.common.base.Joiner.appendTo(Joiner.java:111) at com.google.common.base.Joiner.appendTo(Joiner.java:152) at com.google.common.base.Joiner.join(Joiner.java:193) at com.google.common.base.Joiner.join(Joiner.java:183) at com.google.common.base.Joiner.join(Joiner.java:209) at guava.JoinerEx.main(JoinerEx.java:9)
You can
handle null values in two ways.
a.
By
skipping null values
b.
By
using separate string for null values
import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner1 = Joiner.on("$").skipNulls(); Joiner joiner2 = Joiner.on("$").useForNull("NoData"); String result1 = joiner1.join(null, "1234", "Hello", null, "How"); String result2 = joiner2.join(null, "1234", "Hello", null, "How"); System.out.println(result1); System.out.println(result2); } }
Output
1234$Hello$How
NoData$1234$Hello$NoData$How
Joiner.on("$").skipNulls();
Above
statement creates Joiner instance, while concatenating strings, it simply
ignore null values.
Joiner.on("$").useForNull("NoData");
Above
statement creates Joiner instance, while concatenating, it replace null values
with string “NoData”.
One
important thing to remember is Joiner is immutable and thread safe.
One useful
feature is Joiner class iterates through all kinds of collections that
implements java.lang.Iterable interface and concatenate the strings.
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner = Joiner.on("$").skipNulls(); List<String> employeeList = new ArrayList<>(); employeeList.add("Sankalp Dubey"); employeeList.add("Arpan"); employeeList.add("Sunil Kumar"); employeeList.add("Srinath"); Set<String> employeeSet = new TreeSet<>(employeeList); String result1 = joiner.join(employeeList); String result2 = joiner.join(employeeSet); String result3 = joiner.join(employeeList, employeeSet); System.out.println(result1); System.out.println(result2); System.out.println(result3); } }
Output
Sankalp Dubey$Arpan$Sunil Kumar$Srinath Arpan$Sankalp Dubey$Srinath$Sunil Kumar [Sankalp Dubey, Arpan, Sunil Kumar, Srinath]$[Arpan, Sankalp Dubey, Srinath, Sunil Kumar]
Joining Map data
import java.util.HashMap; import java.util.Map; import com.google.common.base.Joiner; import com.google.common.base.Joiner.MapJoiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner = Joiner.on("$"); Map<Integer, String> employee = new HashMap<>(); employee.put(1, "Hari Krishna Gurram"); employee.put(2, "Arpan Debroy"); employee.put(3, "Sujatha"); employee.put(4, "Preethi Nair"); MapJoiner mapJoiner = joiner.withKeyValueSeparator("="); String result = mapJoiner.join(employee); System.out.println(result); } }
Output
1=Hari Krishna Gurram$2=Arpan Debroy$3=Sujatha$4=Preethi Nair
Write concatenated data to StringBuilder
By using appendTo
method you can write concatenated data to StringBuilder.
import java.util.ArrayList; import java.util.List; import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) { Joiner joiner = Joiner.on("$"); StringBuilder builder = new StringBuilder(); List<String> employeeList = new ArrayList<>(); employeeList.add("Sankalp Dubey"); employeeList.add("Arpan"); employeeList.add("Sunil Kumar"); employeeList.add("Srinath"); joiner.appendTo(builder, employeeList); System.out.println(builder); } }
Output
Sankalp Dubey$Arpan$Sunil Kumar$Srinath
Write
concatenated data to any Appendable interface
Implementations like
BufferedWriter, CharArrayWriter, CharBuffer, FileWriter, FilterWriter,
LogStream, OutputStreamWriter, PipedWriter, PrintStream, PrintWriter,
StringBuffer, StringBuilder, StringWriter, Writer
Writing concatenated data to a file
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.common.base.Joiner; public class JoinerEx { public static void main(String args[]) throws IOException { Joiner joiner = Joiner.on("$"); StringBuilder builder = new StringBuilder(); List<String> employeeList = new ArrayList<>(); employeeList.add("Sankalp Dubey"); employeeList.add("Arpan"); employeeList.add("Sunil Kumar"); employeeList.add("Srinath"); FileWriter writer = new FileWriter(new File( "/Users/harikrishna_gurram/out.txt")); joiner.appendTo(writer, employeeList); writer.close(); System.out.println(builder); } }
Above
program writes “Sankalp Dubey$Arpan$Sunil Kumar$Srinath” to "/Users/harikrishna_gurram/out.txt".
No comments:
Post a Comment