Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>[Didn't handle trailing and leading nulls/empties gracefully. Now works better.]</em></p> <p>My take on it, using Google Guava (not official Java, but a darn good set of packages). I'm offering it since it appears that you looked at using Joiner, but then rejected it. So since you were open to using Joiner at one point, maybe you want to look at it again:</p> <pre><code>package testCode; import java.util.List; import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; public class TestClass { Joiner joinComma = Joiner.on(", "); Joiner joinAndForTwo = Joiner.on(" and "); Joiner joinAndForMoreThanTwo = Joiner.on(", and "); public String joinWithAnd(List&lt;String&gt; elements) { ImmutableList&lt;String&gt; elementsNoNullsOrEmpties = new ImmutableList.Builder&lt;String&gt;() .addAll(Iterables.filter(elements, new Predicate&lt;String&gt;() { @Override public boolean apply(String arg0) { return !Strings.isNullOrEmpty(arg0); } })).build(); if (elementsNoNullsOrEmpties.size() == 0) { return null; } else if (elementsNoNullsOrEmpties.size() == 1) { return Iterables.getOnlyElement(elementsNoNullsOrEmpties); } else if (elementsNoNullsOrEmpties.size() == 2) { return joinAndForTwo.join(elementsNoNullsOrEmpties); } else { final List&lt;String&gt; leadingElements = elementsNoNullsOrEmpties .subList(0, elementsNoNullsOrEmpties.size() - 1); final String trailingElement = elementsNoNullsOrEmpties .get(elementsNoNullsOrEmpties.size() - 1); return joinAndForMoreThanTwo.join(joinComma.join(leadingElements), trailingElement); } } } </code></pre> <p>And the test driver:</p> <pre><code>package testCode; import java.util.List; import com.google.common.collect.Lists; public class TestMain { static List&lt;String&gt; test1 = Lists.newArrayList(); static List&lt;String&gt; test2 = Lists.newArrayList(""); static List&lt;String&gt; test3 = Lists.newArrayList("a"); static List&lt;String&gt; test4 = Lists.newArrayList("a", "b"); static List&lt;String&gt; test5 = Lists.newArrayList("a", "b", "c", "d"); static List&lt;String&gt; test6 = Lists.newArrayList("a", "b", "c", null, "d"); static List&lt;String&gt; test7 = Lists.newArrayList("a", "b", "c", null); static List&lt;String&gt; test8 = Lists.newArrayList("a", "b", "", "", null, "c", null); static List&lt;String&gt; test9 = Lists.newArrayList("", "a", "b", "c", null); static List&lt;String&gt; test10 = Lists.newArrayList(null, "a", "b", "c", null); public static void main(String[] args) { TestClass testClass = new TestClass(); System.out.println(testClass.joinWithAnd(test1)); System.out.println(testClass.joinWithAnd(test2)); System.out.println(testClass.joinWithAnd(test3)); System.out.println(testClass.joinWithAnd(test4)); System.out.println(testClass.joinWithAnd(test5)); System.out.println(testClass.joinWithAnd(test6)); System.out.println(testClass.joinWithAnd(test7)); System.out.println(testClass.joinWithAnd(test8)); System.out.println(testClass.joinWithAnd(test9)); System.out.println(testClass.joinWithAnd(test10)); } } </code></pre> <p>And the output:</p> <p>null<br/> null<br/> a<br/> a and b<br/> a, b, c, and d<br/> a, b, c, and d<br/> a, b, and c<br/> a, b, and c<br/> a, b, and c<br/> a, b, and c<br/></p> <p>I like this because it doesn't do any string splicing. It partitions the provided list of strings, and then correctly glues them together, using rules based on the number of string elements, without going back and backfitting an "and" after the fact. I also handle all sorts of edge cases for nulls/empties appearing at the beginning, end, or middle of the list of strings. It might be that you're guaranteed that this won't happen, so you can simplify this solution.</p> <p><em>[Mine is a bit different from yours in that when I have exactly two elements, I don't put a comma after the first element and before the "and", while for three or more, there is a comma before the "and". It's a style thing. Easy to adjust to whatever you prefer with regards to how commas ought to work.]</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload