title: Joining a List<String> in Java with commas and "and"
body: <p>Given a list</p>
  
  <pre><code>List&lt;String&gt; l = new ArrayList&lt;String&gt;();
  l.add("one");
  l.add("two");
  l.add("three");
  </code></pre>
  
  <p>I have a method</p>
  
  <pre><code>String join(List&lt;String&gt; messages) {
          if (messages.isEmpty()) return "";
          if (messages.size() == 1) return messages.get(0);
          String message = "";
          message = StringUtils.join(messages.subList(0, messages.size() -2), ", ");
          message = message + (messages.size() &gt; 2 ? ", " : "") + StringUtils.join(messages.subList(messages.size() -2, messages.size()), ", and ");
          return message;
      }
  </code></pre>
  
  <p>which, for l, produces "one, two, and three".
  My question is, is there a standard (apache-commons) method that does the same?, eg</p>
  
  <pre><code>WhatEverUtils.join(l, ", ", ", and ");
  </code></pre>
  
  <p>To clarify. My problem is not getting this method to work. It works just as I want it to, it's tested and all is well. My problem is that I could not find some apache-commons-like module which implements such functionality. Which surprises me, since I cannot be the first one to need this.</p>
  
  <p>But then maybe everyone else has just done</p>
  
  <pre><code>StringUtils.join(l, ", ").replaceAll(lastCommaRegex, ", and");
  </code></pre>