Copy the given Collection into a String array. - Java java.lang

Java examples for java.lang:String Array

Introduction

The following code shows how to Copy the given Collection into a String array. .

Demo Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from  w  ww  .j  av a2  s . c  o m*/
     * Copy the given Collection into a String array. The Collection must
     * contain String elements only.
     * 
     * @param collection
     *            the Collection to copy
     * @return the String array ({@code null} if the passed-in Collection was
     *         {@code null})
     */
    public static String[] toStringArray(Collection<String> collection) {
        if (collection == null) {
            return null;
        }
        return collection.toArray(new String[collection.size()]);
    }
}

Related Tutorials