Java List Concatenate concatGenericList(List list, String delimiter)

Here you can find the source of concatGenericList(List list, String delimiter)

Description

Concatenate a list of objects into a single string.

License

Open Source License

Parameter

Parameter Description
list objects toString() representation in the list will be concatenated
delimiter this will be printed between the pieces. Will not be printed before and after them.

Return

A single string that contains the input list concatenated with delimiters in between

Declaration

public static String concatGenericList(List<?> list, String delimiter) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**/*ww  w.jav a2 s  . c o m*/
     * Concatenate a list of objects into a 
     * single string. Objects are represented by their toString method
     * @param list objects toString() representation in the list will be concatenated
     * @param delimiter this will be printed between the pieces. Will not be printed before and after them.
     * @return A single string that contains the input list concatenated with delimiters in between
     */
    public static String concatGenericList(List<?> list, String delimiter) {
        StringBuilder ret = new StringBuilder();
        boolean first = true;
        for (Object s : list) {
            if (!first) {
                ret.append(delimiter);
            }
            ret.append("" + s);
            first = false;
        }
        return ret.toString();
    }
}

Related

  1. concatenateListOfStrings(List list, String separator)
  2. concatenateLists(List... lists)
  3. concatenateParameters(List parameters)
  4. concatenateStrings(List strings, String separator)
  5. concatenateUniqueLists(List first, List second)
  6. concatinateStrings(List strList, String delim)
  7. concatIntegersToRanges( List damages)
  8. concatLines(List lines)
  9. concatLines(List lines)