Convert Object array to string Array - Android java.lang

Android examples for java.lang:array

Description

Convert Object array to string Array

Demo Code

/**/*from   w  w  w.  ja va  2 s  . c  om*/
 * Copyright (c) 2011 Loganalysis team and contributors
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Raffael Schmid - initial API and implementation
 */
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Main{
    public static void main(String[] argv){
        Object[] objects = new String[]{"1","abc","level",null,"book2s.com","asdf 123"};
        System.out.println(java.util.Arrays.toString(stringArray(objects)));
    }
    public static String[] stringArray(final Object[] objects) {
        final List<String> list = collect(Arrays.asList(objects),
                new ClosureIO<Object, String>() {
                    public String call(final Object in) {
                        return String.valueOf(in);
                    }
                });
        return list.toArray(new String[list.size()]);
    }
    public static <I, O> List<O> collect(final List<I> list,
            final ClosureIO<I, O> closure) {
        final List<O> retVal = new ArrayList<O>();
        if (list != null) {
            for (final I item : list) {
                retVal.add(closure.call(item));
            }
        }
        return retVal;
    }
    public static String[] toArray(final List<String> values) {
        return values.toArray(new String[values.size()]);
    }
}

Related Tutorials