Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:05:12+00:00 2026-05-26T10:05:12+00:00

I’m looking for something to augment the function of the apache commons join() function,

  • 0

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”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:05:13+00:00Added an answer on May 26, 2026 at 10:05 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.