Create a modifiable list from the Collection provided. - Android java.util

Android examples for java.util:Collection

Description

Create a modifiable list from the Collection provided.

Demo Code

/*/* w ww.  j a v a  2s.com*/
 * --------------------
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * --------------------
 */
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main{
    public static void main(String[] argv){
        Collection c = java.util.Arrays.asList("asdf","book2s.com");
        System.out.println(newList(c));
    }
    /**
     * Create a modifiable list from the {@link Collection} provided. The return
     * value is backed by an {@link ArrayList}.
     */
    public static <T> List<T> newList(Collection<? extends T> c) {
        return new ArrayList<T>(CollectionUtil.nullAsEmpty(c));
    }
    /**
     * Create a modifiable list from the arguments. The return value is backed
     * by an {@link ArrayList}.
     */
    public static <T> List<T> newList(T... arr) {
        List<T> ret = new ArrayList<T>();
        if (arr != null && arr.length != 0) {
            for (T t : arr) {
                ret.add(t);
            }
        }
        return ret;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashSet}.
     *
     * if the parameter <strong>c</strong> is <strong>null</strong>.
     *
     * @param c
     *            collection to check
     * @param <T>
     *            the type of the collection
     * @return if null new {@link HashSet} otherwise the parameter that was
     *         passed in or
     */
    public static <T> Collection<T> nullAsEmpty(Collection<T> c) {
        return c == null ? new HashSet<T>() : c;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashMap} if the parameter <code>map</code> is <code>null</code>.
     * Otherwise return the parameter that was passed in.
     */
    public static <T, K> Map<T, K> nullAsEmpty(Map<T, K> map) {
        return (map == null) ? new HashMap<T, K>() : map;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashSet} if the parameter <code>set</code> is <code>null</code>.
     * Otherwise return the parameter that was passed in.
     */
    public static <T> Set<T> nullAsEmpty(Set<T> set) {
        return (set == null) ? new HashSet<T>() : set;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link ArrayList} if the parameter <code>list</code> is <code>null</code>
     * . Otherwise return the parameter that was passed in.
     */
    public static <T> List<T> nullAsEmpty(final List<T> list) {
        return (list == null) ? new ArrayList<T>() : list;
    }
}

Related Tutorials