Java List to String listToStr(Collection a, char sep)

Here you can find the source of listToStr(Collection a, char sep)

Description

list To Str

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static String listToStr(Collection a, char sep) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2009 Oracle. All rights reserved.
 * 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.
 * /*  w  w w  .  ja v a  2 s.c  o  m*/
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    @SuppressWarnings("unchecked")
    public static String listToStr(Collection a, char sep) {
        return listToStr(a, String.valueOf(sep));
    }

    public static String listToStr(Collection<Object> a, String sep) {
        //reverse of strToList
        if (a == null)
            return null;
        int count = a.size();
        if (count == 0)
            return null;

        StringBuffer buffer = null;
        for (Iterator<Object> iter = a.iterator(); iter.hasNext();) {
            Object obj = iter.next();
            if (obj == null)
                continue;

            if (buffer == null)
                buffer = new StringBuffer();
            else
                buffer.append(sep);
            if (obj instanceof String)
                buffer.append((String) obj);
            else
                buffer.append(obj);
        }
        return (buffer != null) ? buffer.toString() : null;
    }
}

Related

  1. convertListToString(List listStr, String separator)
  2. convertListToString(List strings)
  3. convertListToString(List strList)
  4. convertListToString(List valueList)
  5. convertListToStringWithSeparator( List myList, String separator)
  6. listToStr(Collection c, String delim)
  7. listToStr(List lst)
  8. listToStr(List list)
  9. listToStr(List stringList)