Java List to String toStringDelimited(final Iterable list, final String delimiter)

Here you can find the source of toStringDelimited(final Iterable list, final String delimiter)

Description

Performs a toString on each element given iterable with a given delimiter between elements.

License

Open Source License

Parameter

Parameter Description
list The list to call toString on each element for.
delimiter The delimiter.

Return

A string with the toString on each element in the list called with a given delimiter between elements. If null is given, then "null" is returned. If an empty list is given, "" is returned.

Declaration

public static String toStringDelimited(final Iterable<?> list, final String delimiter) 

Method Source Code

//package com.java2s;
/*/*from w w  w.j  a  va  2s .  co  m*/
 * File:                CollectionUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright March 25, 2008, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 * See CopyrightHistory.txt for complete details.
 *
 */

import java.util.Iterator;

public class Main {
    /**
     * Performs a toString on each element given iterable with a given delimiter
     * between elements.
     *
     * @param list The list to call toString on each element for.
     * @param delimiter The delimiter.
     * @return A string with the toString on each element in the list called
     * with a given delimiter between elements. If null is given, then "null" is
     * returned. If an empty list is given, "" is returned.
     */
    public static String toStringDelimited(final Iterable<?> list, final String delimiter) {
        if (list == null) {
            return "null";
        }

        final StringBuffer result = new StringBuffer();
        final Iterator<?> iterator = list.iterator();

        if (iterator.hasNext()) {
            result.append(iterator.next());
        }

        while (iterator.hasNext()) {
            result.append(delimiter);
            result.append(iterator.next());
        }

        return result.toString();
    }
}

Related

  1. toString(List strings)
  2. toString(List list)
  3. toString(List list)
  4. toString(List list)
  5. toStringClassList(List list)
  6. toStringItem(List> itemMapList, String subjectName)
  7. toStringList(Collection strings, String separator)
  8. toStringList(final double[] array)
  9. toStringList(final E[] array)