Java Collection to String collectionToString(Collection collection, String delim)

Here you can find the source of collectionToString(Collection collection, String delim)

Description

collection To String

License

Open Source License

Declaration

public static String collectionToString(Collection collection, String delim) 

Method Source Code

//package com.java2s;
/*/*  www. j a v  a2s  .  c  o  m*/
 * 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.Collection;
import java.util.Iterator;

public class Main {
    public static String collectionToString(Collection collection, String delim) {
        if (collection == null) {
            return "NULL";
        }

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

    public static String collectionToString(Collection collection) {
        return collectionToString(collection, ",");
    }

    /**
     * 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. collectionToString(Collection c)
  2. collectionToString(Collection c, String delim)
  3. collectionToString(Collection c, String spilt)
  4. collectionToString(Collection col)
  5. collectionToString(Collection coll)
  6. collectionToString(Collection list, String split)
  7. collectionToString(Collection set)
  8. collectionToString(Collection c)
  9. collectionToString(Collection collection, String delimiter)