Java Collection Create toCollection(final char[] charArray)

Here you can find the source of toCollection(final char[] charArray)

Description

to Collection

License

Apache License

Declaration

public static Collection<? extends String> toCollection(final char[] charArray) 

Method Source Code

//package com.java2s;
/*//  w w  w  . j a  va 2s .  c o 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.ArrayList;
import java.util.Collection;

public class Main {
    /** Constant representing the empty string, equal to &quot;&quot; */
    public static final String EMPTY_STRING = "";

    public static Collection<? extends String> toCollection(final char[] charArray) {
        final Collection<String> c = new ArrayList<String>();
        for (final Object obj : charArray) {
            c.add(obj.toString());
        }
        return c;
    }

    public static Collection<? extends String> toCollection(final Object[] charArray) {
        final Collection<String> c = new ArrayList<String>();
        for (final Object obj : charArray) {
            c.add(obj.toString());
        }
        return c;
    }

    /**
     * Returns the specified array as a comma-delimited (',') string.
     * 
     * @param array
     *            the array whose contents will be converted to a string.
     * @return the array's contents as a comma-delimited (',') string.
     * @since 1.0
     */
    public static String toString(final Object[] array) {
        return toDelimitedString(array, ",");
    }

    /**
     * Returns the array's contents as a string, with each element delimited by
     * the specified {@code delimiter} argument. Useful for {@code toString()}
     * implementations and log messages.
     * 
     * @param array
     *            the array whose contents will be converted to a string
     * @param delimiter
     *            the delimiter to use between each element
     * @return a single string, delimited by the specified {@code delimiter}.
     * @since 1.0
     */
    public static String toDelimitedString(final Object[] array, final String delimiter) {
        if ((array == null) || (array.length == 0)) {
            return EMPTY_STRING;
        }
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                sb.append(delimiter);
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }

    /**
     * Similar to the other method, but allows a variable list of objects.
     * 
     * @param delimiter
     * @param array
     * @return
     */
    public static String toDelimitedString(final String delimiter, final Object... array) {
        if ((array == null) || (array.length == 0)) {
            return EMPTY_STRING;
        }
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                sb.append(delimiter);
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. stringToCollection(String string)
  2. stringToCollection(String string, String delim)
  3. toCollection(C c, E... elements)
  4. toCollection(Class cls, Iterable iterable)
  5. toCollection(Class cls, T[] array)
  6. toCollection(final Iterable iterable)
  7. toCollection(final T[] elements)
  8. toCollection(Iterable iterable)
  9. toCollection(Iterable i)