Java Collection to String collectionToString(Collection c)

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

Description

Transforms a collection to a string representation as same as use of method collectionToString(c, null)

License

Apache License

Parameter

Parameter Description
c a collection.

Return

string representations like above.

Declaration

public static String collectionToString(Collection<?> c) 

Method Source Code


//package com.java2s;
/*//from w w w  .ja v a  2s . c o  m
 * CollectionUtil.java
 *
 * 15.09.2009
 *
 * Copyright 2009 Michael Lieshoff
 *
 *  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
 *
 *    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.*;

public class Main {
    /**
     * Transforms a collection to a string representation as same as use of method
     * collectionToString(c, null)
     *
     * @param c a collection.
     * @return string representations like above.
     */
    public static String collectionToString(Collection<?> c) {
        return collectionToString(c, null);
    }

    /**
     * Transforms a collection to a string representation as same as use of method
     * collectionToString(c, mainTemplate, null)
     *
     * @param c a collection.
     * @param mainTemplate template used as main.
     * @return string representations like above.
     */
    public static String collectionToString(Collection<?> c, String mainTemplate) {
        return collectionToString(c, mainTemplate, null);
    }

    /**
     * Transforms a collection to a string representation as same as use of method
     * collectionToString(c, mainTemplate, template, null)
     *
     * @param c a collection.
     * @param mainTemplate template used as main.
     * @param template template used for the inner elements.
     * @return string representations like above.
     */
    public static String collectionToString(Collection<?> c, String mainTemplate, String template) {
        return collectionToString(c, mainTemplate, template, null);
    }

    /**
     * Transforms a collection to a string representation as same as use of method
     * collectionToString(c, mainTemplate, template, delimiter, null)
     *
     * @param c a collection.
     * @param mainTemplate template used as main.
     * @param template template used for the inner elements.
     * @param delimiter delimiter used for separate inner elements.
     * @return string representations like above.
     */
    public static String collectionToString(Collection<?> c, String mainTemplate, String template,
            String delimiter) {
        return collectionToString(c, mainTemplate, template, delimiter, null);
    }

    /**
     * Transforms a collection to a string representation as same as use of method.
     * <p>Example: Collection {1, 2, 3, 4}<p>
     * mainTemplate = [%1] ==> [1234]<p>
     * template = [%1] ==> [[1][2][3][4]]<p>
     * delimiter = , ==> [[1],[2],[3],[4]]<p>
     *
     * @param c a collection.
     * @param mainTemplate template used as main.
     * @param template template used for the inner elements.
     * @param delimiter delimiter used for separate inner elements.
     * @param evenDelimiter delimiter used for separate even inner elements.
     * @return string representations like above.
     */
    public static String collectionToString(Collection<?> c, String mainTemplate, String template, String delimiter,
            String evenDelimiter) {
        if (c == null || c.size() == 0) {
            return "";
        }
        boolean deli = delimiter != null && delimiter.length() > 0;
        boolean edeli = evenDelimiter != null && evenDelimiter.length() > 0;
        boolean templ = template != null && template.length() > 0;
        StringBuilder s = new StringBuilder();
        int z = 0;
        for (Iterator<?> i = c.iterator(); i.hasNext();) {
            Object o = i.next();
            if (templ) {
                s.append(template.replace("%1", String.valueOf(o)));
            } else {
                s.append(String.valueOf(o));
            }
            if (z % 2 == 0 && i.hasNext() && edeli) {
                s.append(evenDelimiter);
            } else if (deli && i.hasNext()) {
                s.append(delimiter);
            }
            z++;
        }
        boolean mainTempl = mainTemplate != null && mainTemplate.length() > 0;
        if (mainTempl) {
            return mainTemplate.replace("%1", s.toString());
        }
        return s.toString();
    }
}

Related

  1. collectionToString(Collection c)
  2. collectionToString(Collection collection, String delimiter)
  3. collectionToString(Collection collection, String seperator)
  4. collectionToString(Collection list, String delimiter)
  5. collectionToString(Collection c)
  6. collectionToString(Collection c)
  7. collectionToString(Collection c)
  8. collectionToString(Collection c, String separator)
  9. collectionToString(Collection coll, String connectSymbol)