Java List to String listToString(List list, String delim)

Here you can find the source of listToString(List list, String delim)

Description

Print out a List in a user-friendly string format.

License

Open Source License

Parameter

Parameter Description
list A List to print out.
delim The delimiter to use between elements.

Return

The List in a user-friendly string format.

Declaration

public static String listToString(List list, String delim) 

Method Source Code

//package com.java2s;
/*/*from   w ww .java2 s  .  c om*/
 * RHQ Management Platform
 * Copyright (C) 2005-2008 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation, and/or the GNU Lesser
 * General Public License, version 2.1, also as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License and the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

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

public class Main {
    /**
     * Print out a List in a user-friendly string format.
     *
     * @param  list  A List to print out.
     * @param  delim The delimiter to use between elements.
     *
     * @return The List in a user-friendly string format.
     */
    public static String listToString(List list, String delim) {
        if (list == null) {
            return "NULL";
        }

        Iterator i = list.iterator();
        return iteratorToString(i, delim, null);
    }

    /**
     * Print out a List in a user-friendly string format.
     *
     * @param  list A List to print out.
     *
     * @return The List in a user-friendly string format.
     */
    public static String listToString(List list) {
        return listToString(list, ",");
    }

    /**
     * Print out everything in an Iterator in a user-friendly string format.
     *
     * @param  i     An iterator to print out.
     * @param  delim The delimiter to use between elements.
     *
     * @return The Iterator's elements in a user-friendly string format.
     */
    public static String iteratorToString(Iterator i, String delim) {
        return iteratorToString(i, delim, "");
    }

    /**
     * Print out everything in an Iterator in a user-friendly string format.
     *
     * @param  i         An iterator to print out.
     * @param  delim     The delimiter to use between elements.
     * @param  quoteChar The character to quote each element with.
     *
     * @return The Iterator's elements in a user-friendly string format.
     */
    public static String iteratorToString(Iterator i, String delim, String quoteChar) {
        Object elt = null;
        StringBuilder rstr = new StringBuilder();
        String s;

        while (i.hasNext()) {
            if (rstr.length() > 0) {
                rstr.append(delim);
            }

            elt = i.next();
            if (elt == null) {
                rstr.append("NULL");
            } else {
                s = elt.toString();
                if (quoteChar != null) {
                    rstr.append(quoteChar).append(s).append(quoteChar);
                } else {
                    rstr.append(s);
                }
            }
        }

        return rstr.toString();
    }
}

Related

  1. listToString(List l)
  2. listToString(List list)
  3. listToString(List list)
  4. listToString(List list)
  5. listToString(List list, char sep)
  6. listToString(List list, String glue)
  7. listToString(List list, String prefix, String suffix, String separator)
  8. listToString(List list)
  9. listToString(List list)