Java String Join Join(ArrayList list, String delimiter)

Here you can find the source of Join(ArrayList list, String delimiter)

Description

This method converts a list of String objects to a single String representation and inserts a delimiter between each object.

License

Open Source License

Parameter

Parameter Description
list List of String objects
delimiter Delimiter

Return

Formatted String representation

Declaration

public static String Join(ArrayList<String> list, String delimiter) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.*;

public class Main {
    /** This method converts an array of String objects to a single String representation and inserts a delimiter between each object.
     */*from   w  w w  . ja  va  2  s.co  m*/
     * @param s Array of String objects
     * @param delimiter Delimiter
     * @return Formatted String representation
     */
    public static String Join(String[] s, String delimiter) {
        return Join(CreateStringList(s), delimiter);
    }

    /** This method converts a list of String objects to a single String representation and inserts a delimiter between each object.
     * 
     * @param list List of String objects
     * @param delimiter Delimiter
     * @return Formatted String representation
     */
    public static String Join(ArrayList<String> list, String delimiter) {
        if (list.isEmpty())
            return "";

        StringBuilder sb = new StringBuilder();

        for (String x : list)
            sb.append(x + delimiter);

        sb.delete(sb.length() - delimiter.length(), sb.length());

        return sb.toString();
    }

    /** Converts an array of objects to a list of String objects.
     *
     * @param values Array of objects
     * @return List of String objects
     */
    public static ArrayList<String> CreateStringList(Object... values) {
        return CreateStringList(CreateObjectList(values));
    }

    /** Converts an array of String objects to a list of String objects.
     *
     * @param values Array of String objects
     * @return List of String objects
     */
    public static ArrayList<String> CreateStringList(String... values) {
        ArrayList<String> results = new ArrayList<String>();
        if (values.length > 0)
            Collections.addAll(results, values);
        return results;
    }

    /** This method iterates over multiple lists of String objects and creates a single list of String objects containing the contents of all.
     *
     * @param lists Array of String lists
     * @return Combined list
     */
    public static ArrayList<String> CreateStringList(ArrayList<String>... lists) {
        ArrayList<String> all = new ArrayList<String>();

        for (ArrayList<String> list : lists)
            all.addAll(list);

        return new ArrayList<String>(all);
    }

    /** Converts a generic collection of objects into a list of String objects.
     *
     * @param values Collection of objects
     * @return List of String objects
     */
    public static ArrayList<String> CreateStringList(Collection values) {
        ArrayList<String> results = new ArrayList<String>();

        for (Object value : values)
            results.add(String.valueOf(value));

        return results;
    }

    /** Creates a list of String objects.
     *
     * @param value Value to add to list
     * @param numRepetitions Number of times to add value to list
     * @return List of String values
     */
    public static ArrayList<String> CreateStringList(String value, int numRepetitions) {
        ArrayList<String> list = new ArrayList<String>();

        for (int i = 0; i < numRepetitions; i++)
            list.add(value);

        return list;
    }

    /** Creates a list of objects from an array of objects.
     *
     * @param values Array of objects
     * @return List of objects
     */
    public static ArrayList CreateObjectList(Object... values) {
        ArrayList results = new ArrayList();
        Collections.addAll(results, values);
        return results;
    }
}

Related

  1. invertPhoneNumber(String storedNumber, String joinString)
  2. join(boolean endSlash, Collection paths)
  3. join(Collection collection, String separator)
  4. join(Collection items, String delimiter)
  5. join(Enumeration enumeration, String s)