Java List Concatenate concatenateList(List list, String separator)

Here you can find the source of concatenateList(List list, String separator)

Description

Concatenates the list of Objects using the provided separator

License

Open Source License

Parameter

Parameter Description
list the list to concatenate
separator the separator used to concatenate the elements of the list

Return

A string with the string representation of that objects

Declaration

public static String concatenateList(List<? extends Object> list, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 - 2013 by Timotei Dolean <timotei21@gmail.com>
 * /*from ww  w.  j a  v a  2  s . c o  m*/
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Concatenates the list of Objects using the provided separator
     * 
     * @param list
     *        the list to concatenate
     * @param separator
     *        the separator used to concatenate the elements of the list
     * @return A string with the string representation of that objects
     */
    public static String concatenateList(List<? extends Object> list, String separator) {
        if (list == null || list.isEmpty()) {
            return ""; //$NON-NLS-1$
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < list.size() - 1; i++) {
            result.append(list.get(i) + separator);
        }
        result.append(list.get(list.size() - 1));

        return result.toString();
    }
}

Related

  1. concatenate(List... lists)
  2. concatenate(String[] selectors, List lessExtends)
  3. concatenateFileContent(List fileContents)
  4. ConcatenateInt(List arrays)
  5. concatenateList(List values)
  6. concatenateListOfStrings(List list, String separator)
  7. concatenateLists(List... lists)
  8. concatenateParameters(List parameters)
  9. concatenateStrings(List strings, String separator)