Returns a list of strings from all collection of arbitrary objects. - Android java.util

Android examples for java.util:Collection

Description

Returns a list of strings from all collection of arbitrary objects.

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        Collection coll = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(toStringList(coll));
    }//from  ww w.  j  ava  2  s  . c  om

    /**
     * Returns a list of strings from al collection of arbitrary objects.
     *
     * Uses the <code>toString()</code> operation of every collection element.
     * Null elements set to null in the list.
     *
     * @param  coll collection
     * @return      list of strings
     */
    public static List<String> toStringList(Collection<?> coll) {
        if (coll == null) {
            throw new NullPointerException("coll == null");
        }

        List<String> list = new ArrayList<>(coll.size());

        for (Object o : coll) {
            if (o == null) {
                list.add(null);
            } else {
                list.add(o.toString());
            }
        }

        return list;
    }
}

Related Tutorials