Java Collection to String collectionToString(Collection coll)

Here you can find the source of collectionToString(Collection coll)

Description

Converts the given collection of objects to string as a comma-separated list.

License

Open Source License

Parameter

Parameter Description
coll collection of objects to be converted

Return

A string containing each object in the given collection, converted toString() and separated by commas.

Declaration

public static String collectionToString(Collection coll) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2003 Andy Jefferson and others. All rights reserved. 
Licensed 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//from  ww  w  .  j a  va 2s  . c om
    
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.
     
    
Contributors:
2003 Erik Bengtson - moved replaceAll from Column class to here
2004 Andy Jefferson - moved intArrayToString, booleanArrayToString from SM
2007 Xuan Baldauf - toJVMIDString hex fix
...
**********************************************************************/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Converts the given collection of objects to string as a comma-separated
     * list.  If the list is empty the string "<none>" is returned.
     *
     * @param coll collection of objects to be converted
     * @return  A string containing each object in the given collection,
     *          converted toString() and separated by commas.
     */
    public static String collectionToString(Collection coll) {
        if (coll == null) {
            return "<null>";
        } else if (coll.isEmpty()) {
            return "<none>";
        } else {
            StringBuilder s = new StringBuilder();
            Iterator iter = coll.iterator();
            while (iter.hasNext()) {
                Object obj = iter.next();
                if (s.length() > 0) {
                    s.append(", ");
                }
                s.append(obj);
            }

            return s.toString();
        }
    }

    /**
     * check string is null or length is 0.
     * @param s check string 
     * @return return true if string is null or length is 0. return false other case.
     */
    public static boolean isEmpty(String s) {
        return ((s == null) || (s.length() == 0));
    }
}

Related

  1. collectionToString(@SuppressWarnings("rawtypes") Collection coll)
  2. collectionToString(Collection c)
  3. collectionToString(Collection c, String delim)
  4. collectionToString(Collection c, String spilt)
  5. collectionToString(Collection col)
  6. collectionToString(Collection collection, String delim)
  7. collectionToString(Collection list, String split)
  8. collectionToString(Collection set)
  9. collectionToString(Collection c)