Java List from listOfItems(List items, String separator, String finalConjunction)

Here you can find the source of listOfItems(List items, String separator, String finalConjunction)

Description

Return a string that is a list of items separated by separator and using the final conjunction.

License

Apache License

Parameter

Parameter Description
items a parameter
separator a parameter
finalConjunction a parameter

Return

a list of items

Declaration

public static String listOfItems(List<String> items, String separator,
        String finalConjunction) 

Method Source Code

//package com.java2s;
/*//from  w  w w.j a va2  s.c o  m
 *   Copyright 2006-2016, Rathravane LLC
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *   
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Return a string that is a list of items separated by separator and using
     * the final conjunction. For example, { "A", "B", "C" } --&gt; "A, B, and C"
     * @param items
     * @param separator
     * @param finalConjunction
     * @return a list of items
     */
    public static String listOfItems(List<String> items, String separator,
            String finalConjunction) {
        final int size = items.size();
        if (size < 1)
            return "";

        switch (size) {
        case 1:
            return items.iterator().next();

        case 2:
            return items.get(0) + " " + finalConjunction + " "
                    + items.get(1);

        default: {
            final StringBuffer result = new StringBuffer();
            for (int i = 0; i < size - 1; i++) {
                result.append(items.get(i));
                result.append(separator);
            }
            result.append(finalConjunction);
            result.append(" ");
            result.append(items.get(size - 1));
            return result.toString();
        }
        }
    }
}

Related

  1. listOf(T... elems)
  2. listOf(T... values)
  3. listOfArrays(T[]... values)
  4. listOfExceptionsAsString(List exs)
  5. listOfIntegers2ArrayOfInts(List xs)
  6. listOfOne()
  7. listOfOne(T x)
  8. listOfStrings(int size, String prefix)
  9. listOfStrToString(List src, String del)