{"id":35616,"date":"2016-06-22T10:22:23","date_gmt":"2016-06-22T04:52:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=35616"},"modified":"2016-06-22T10:22:23","modified_gmt":"2016-06-22T04:52:23","slug":"stringjoiner-in-java-8","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/stringjoiner-in-java-8\/","title":{"rendered":"StringJoiner in Java 8"},"content":{"rendered":"<p>Being a programmer, at some point of time we encountered a situation in which you wanted to join (not concat) multiple strings of delimited items in Java. Worst can be if you are provided multiple string literals. Just think for a second and how will you build a string of delimited items using these literals. Eg.<\/p>\n<p>[java]<br \/>\nString str1 = &quot;java&quot;;<br \/>\nString str2 = &quot;scala&quot;;<br \/>\nString str3 = &quot;groovy&quot;;<\/p>\n<p>StringBuilder sb = new StringBuilder();<br \/>\nsb.append(str1).append(&quot;,&quot;).append(str2).append(&quot;,&quot;).append(str3);<br \/>\nsb.toString();<\/p>\n<p>[\/java]<\/p>\n<p>Or, many times we find ourselves in a situation where you want to pass a collection to IN clause of HQL or SQL. In this era of programming this is how we do this so obvious work<\/p>\n<p>[java]<br \/>\n\/\/ List&lt;String&gt; listOfNames = &#8230;.<br \/>\n StringBuilder sbObj = new StringBuilder();<br \/>\n for (String str : listOfNames) {<br \/>\n   if (sbObj.length() &gt; 0) {<br \/>\n      sbObj.append(&#8216;,&#8217;);<br \/>\n   }<br \/>\n   sbObj.append(str);<br \/>\n }<br \/>\n sbObj.toString();<\/p>\n<p>[\/java]<\/p>\n<p>Code reviewer will acquit you due to lack of good solutions evidence. But not anymore, In Java 8 we finally can join Strings with ease.<\/p>\n<p>StringJoiner\u00a0is a class added in Java 8. As the name suggests, we can use this class to join strings. There are two constructors, one taking the separator (delimiter) and one allowing a prefix and suffix.<\/p>\n<p><span style=\"color: #333333\">1.\u00a0<strong>StringJoiner(CharSequence delimiter)<\/strong><\/span><\/p>\n<p>[java]<br \/>\nStringJoiner joiner = new StringJoiner(&quot;,&quot;);<br \/>\njoiner.add(&quot;java&quot;);<br \/>\njoiner.add(&quot;scala&quot;);<br \/>\njoiner.add(&quot;groovy&quot;);<br \/>\nString desiredStr = joiner.toString(); \/\/ &quot;java,scala,groovy&quot;<\/p>\n<p>\/\/ add() calls can be chained<br \/>\n joined = new StringJoiner(&quot;-&quot;)<br \/>\n         .add(&quot;java&quot;)<br \/>\n         .add(&quot;scala&quot;)<br \/>\n         .add(&quot;groovy&quot;)<br \/>\n         .toString(); \/\/ &quot;java-scala-groovy&quot;<br \/>\n[\/java]<\/p>\n<p><span style=\"color: #333333\">2.<strong> StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)<\/strong><\/span><\/p>\n<p>The String like &#8220;[Richard:Gilfoyle:Erlich]&#8221; may be constructed as follows:<\/p>\n<p>[java]<br \/>\nStringJoiner sj = new StringJoiner(&quot;:&quot;, &quot;[&quot;, &quot;]&quot;);<br \/>\nsj.add(&quot;Richard&quot;).add(&quot;Gilfoyle&quot;).add(&quot;Erlich&quot;);<br \/>\nString desiredString = sj.toString();<br \/>\n[\/java]<\/p>\n<p>Moreover, StringJoiner is used as a behind scene tool by the two new static<strong><span style=\"color: #333333\"> join()\u00a0<\/span><\/strong>methods of <span style=\"color: #333333\"><strong>String class<\/strong><\/span>:<\/p>\n<p><span style=\"color: #333333\">\/\/ <strong>\u00a0join(CharSequence delimiter, CharSequence&#8230; elements)<\/strong><\/span><\/p>\n<p>[java]<br \/>\n String joined = String.join(&quot;\/&quot;, &quot;2016&quot;, &quot;06&quot;, &quot;15&quot; ); \/\/ &quot;2016\/06\/15&quot;<br \/>\n[\/java]<\/p>\n<p><span style=\"color: #333333\">\/\/<strong> join(CharSequence delimiter, Iterable&lt;? extends CharSequence&gt; elements)<\/strong><\/span><\/p>\n<p>[java]<br \/>\n List&lt;String&gt; list = Arrays.asList(&quot;java&quot;, &quot;scala&quot;, &quot;groovy&quot;);<br \/>\n joined = String.join(&quot;;&quot;, list); \/\/ &quot;java;scala;groovy&quot;<br \/>\n[\/java]<\/p>\n<p>A new joining Collector is also available for the new Stream API:<\/p>\n<p>[java]<br \/>\nList&lt;Employee&gt; list = Arrays.asList(<br \/>\n                        new Employee(&quot;Richard&quot;, &quot;Hendriks&quot;),<br \/>\n                        new Employee(&quot;Erlich&quot;, &quot;Bachmann&quot;),<br \/>\n                        new Employee(&quot;Jared&quot;, &quot;Dunn &quot;)<br \/>\n                      );<\/p>\n<p>String joinedFirstNames = list.stream()<br \/>\n                              .map(Employee::getFirstName)<br \/>\n                              .collect(Collectors.joining(&quot;,&quot;));<\/p>\n<p>\/\/ &quot;Richard, Erlich, Jared&quot;<\/p>\n<p>[\/java]<\/p>\n<p>Now, \u00a0join string smartly \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being a programmer, at some point of time we encountered a situation in which you wanted to join (not concat) multiple strings of delimited items in Java. Worst can be if you are provided multiple string literals. Just think for a second and how will you build a string of delimited items using these literals. [&hellip;]<\/p>\n","protected":false},"author":820,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[4844,1513,3551],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35616"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/820"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=35616"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/35616\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=35616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=35616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=35616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}