Java CSV from listToCsv(List list)

Here you can find the source of listToCsv(List list)

Description

Converts a List of objects to a comma separated string of the objects' string representations in the order given by the List's iterator.

License

Apache License

Parameter

Parameter Description
list a parameter

Return

a List of objects to a comma separated string of the object's string representations

Declaration

public static String listToCsv(List<?> list) 

Method Source Code

//package com.java2s;
/*//from  www.ja va2  s. co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Converts a List of objects to a comma separated string of the objects'
     * string representations in the order given by the List's iterator. For
     * example:
     *
     * <blockquote>
     *
     * <pre>
     *          List list = Arrays.toList(new String[] {&quot;one&quot;, &quot;two&quot;, &quot;three&quot;};
     *          System.out.println(listToCsv(list));
     * </pre>
     *
     * </blockquote> would return the string "one,two,three".
     *
     * @param list
     * @return a List of objects to a comma separated string of the object's
     *         string representations
     */
    public static String listToCsv(List<?> list) {
        StringBuffer sb = new StringBuffer();
        for (Iterator<?> iter = list.iterator(); iter.hasNext();) {
            sb.append(iter.next().toString());
            if (iter.hasNext()) {
                sb.append(',');
            }
        }
        return sb.toString();
    }
}

Related

  1. getPrintableCSVs(List list)
  2. getRightFirstRow(List csvRows)
  3. getStringList(String csv)
  4. getStringListFromCsv(String tags)
  5. getVendorIdsAndNames(String vendorCsv, List vendorNames, List vendorIDs)
  6. listToCsv(List listOfStrings, char separator)
  7. listToCsv(List strs)
  8. ListtoCSV(List thelist, String sep)
  9. listToCSVString(List list)