Java Collection to String toStr(Collection elements)

Here you can find the source of toStr(Collection elements)

Description

Converts the collection into an array of strings.

License

Open Source License

Parameter

Parameter Description
elements a parameter

Declaration

public static String[] toStr(Collection<String> elements) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) Devamatre Technologies 2009
 * // www  . ja v a2  s.c  o m
 * This code is licensed to Devamatre under one or more contributor license 
 * agreements. The reproduction, transmission or use of this code or the 
 * snippet is not permitted without prior express written consent of Devamatre. 
 * 
 * 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 and the 
 * offenders will be liable for any damages. All rights, including  but not
 * limited to rights created by patent grant or registration of a utility model 
 * or design, are reserved. Technical specifications and features are binding 
 * only insofar as they are specifically and expressly agreed upon in a written 
 * contract.
 * 
 * You may obtain a copy of the License for more details at:
 *      http://www.devamatre.com/licenses/license.txt.
 *      
 * Devamatre reserves the right to modify the technical specifications and or 
 * features without any prior notice.
 *****************************************************************************/

import java.util.Collection;

public class Main {
    /**
     * Converts an array of objects into an array of strings.
     * 
     * @param elements
     * @return
     */
    public static String[] toStr(Object... elements) {
        String[] strElements = null;
        if (elements != null) {
            strElements = new String[elements.length];
            for (int i = 0; i < elements.length; i++) {
                if (elements[i] instanceof String) {
                    strElements[i] = elements[i].toString();
                }
            }
        }
        return strElements;
    }

    /**
     * Converts the collection into an array of strings.
     * 
     * @param elements
     * @return
     */
    public static String[] toStr(Collection<String> elements) {
        String[] strElements = null;
        if (elements != null) {
            strElements = new String[elements.size()];
            Object[] oElements = elements.toArray();
            for (int i = 0; i < oElements.length; i++) {
                strElements[i] = oElements[i].toString();
            }
        }
        return strElements;
    }
}

Related

  1. listToStringArray(Collection list)
  2. listToStringHelper(StringBuilder sb, String sep, Collection list)
  3. stringify(Collection collection)
  4. stringify(Collection collection)
  5. toStr(Collection collection)
  6. toString(Collection c)
  7. toString(Collection c)
  8. toString(Collection c, String start, String separator, String end)
  9. toString(Collection collection, String separator, int fixedLength)