Java tutorial
//package com.java2s; /* * 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(); } }