Convert Object List to string List - Android java.util

Android examples for java.util:List Convert

Description

Convert Object List to string List

Demo Code

/**/*from   www.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){
        List list = java.util.Arrays.asList("asdf","book2s.com");
        System.out.println(stringList(list));
    }
    public static <T> List<String> stringList(final List<T> list) {
        return collect(list, new ClosureIO<T, String>() {
            public String call(final T in) {
                return String.valueOf(in);
            }
        });
    }
    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;
    }
}

Related Tutorials