Is there an existing Java utility method that can join a list of Strings on “,” and “and”?

StackOverflow https://stackoverflow.com/questions/7852611

  •  10-02-2021
  •  | 
  •  

Question

I'm looking for something to augment the function of the apache commons join() function, basically that will do what makePrettyList() does

public String makePrettyList(List<String> items) {
    String list = org.apache.commons.lang.StringUtils.join(items, ", ");
    int finalComma = list.lastIndexOf(",");
    return list.substring(0, finalComma) + " and" + list.substring(finalComma + 1, list.length());
}

makePrettyList(["Alpha", "Beta", "Omega"]) --> "Alpha, Beta and Omega"

Was it helpful?

Solution

[Didn't handle trailing and leading nulls/empties gracefully. Now works better.]

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:

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<String> elements) {
        ImmutableList<String> elementsNoNullsOrEmpties = new ImmutableList.Builder<String>()
                .addAll(Iterables.filter(elements, new Predicate<String>() {
                    @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<String> leadingElements = elementsNoNullsOrEmpties
                    .subList(0, elementsNoNullsOrEmpties.size() - 1);
            final String trailingElement = elementsNoNullsOrEmpties
                    .get(elementsNoNullsOrEmpties.size() - 1);
            return joinAndForMoreThanTwo.join(joinComma.join(leadingElements),
                    trailingElement);
        }
    }
}

And the test driver:

package testCode;

import java.util.List;

import com.google.common.collect.Lists;

public class TestMain {

    static List<String> test1 = Lists.newArrayList();
    static List<String> test2 = Lists.newArrayList("");
    static List<String> test3 = Lists.newArrayList("a");
    static List<String> test4 = Lists.newArrayList("a", "b");
    static List<String> test5 = Lists.newArrayList("a", "b", "c", "d");
    static List<String> test6 = Lists.newArrayList("a", "b", "c", null, "d");
    static List<String> test7 = Lists.newArrayList("a", "b", "c", null);
    static List<String> test8 = Lists.newArrayList("a", "b", "", "", null, "c",
            null);
    static List<String> test9 = Lists.newArrayList("", "a", "b", "c", null);
    static List<String> 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));
    }
}

And the output:

null
null
a
a and b
a, b, c, and d
a, b, c, and d
a, b, and c
a, b, and c
a, b, and c
a, b, and c

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.

[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.]

OTHER TIPS

Here is a nice way to decorate/customize only the instances of List<String> where you need them.

import java.util.*;

public class ListUtils
{
    public static void main(final String[] args)
    {
        final List<String> sl = new ArrayList<String>()
        {
            public String toString()
            {
                final StringBuilder sb = new StringBuilder(this.size() * 512);
                final ListIterator<String> li = this.listIterator();
                while (li.hasNext())
                {
                    if (li.nextIndex() == this.size() - 1)
                    {
                        sb.append("and ").append(li.next());
                    }
                    else if (li.nextIndex() == this.size() - 2)
                    {
                        sb.append(li.next()).append(" ");
                    }
                    else
                    {
                        sb.append(li.next()).append(", ");
                    }
                }
                return sb.toString();
            }
        };

        // Test the output  
        sl.add("Alpha");
        sl.add("Beta");
        sl.add("Omega");
        System.out.println(sl.toString());
    }
}

and the output is

Alpha, Beta and Omega

You can put the creation of such a construct in a public static Factory Method and generate these when ever you need them.

If you're not planning to use a 3rd party library, you would need to write your own utility.

public List<String> makePrettyList(List<String> items) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < items.size(); i++) { 
        if (i > 0) {
            builder.append(" ");

            if (i == items.size() - 1) {
                builder.append("and ");
            }
            else {
                builder.append(", ");
            }
        }

        builder.append(items.get(i));
    }

    return builder.toString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top