Android examples for java.util:List
Create an unmodifiable List based on the List passed in checks for null and returns an empty list if null is passed in.
/*/*www . j a v a 2 s . c om*/ * -------------------- * 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){ List list = java.util.Arrays.asList("asdf","book2s.com"); System.out.println(newReadOnlyList(list)); } /** * Create an unmodifiable {@link List} based on the {@link List} passed in * checks for null and returns an empty list if null is passed in. This one * insures that the order is maintained between lists. */ public static <T> List<T> newReadOnlyList(final List<? extends T> list) { List<T> l = new ArrayList<T>(nullAsEmpty(list)); return Collections.unmodifiableList(l); } /** * Creates an unmodifiable {@link List} from a variable number arguments. */ public static <T> List<T> newReadOnlyList(T... obj) { return Collections.unmodifiableList(newList(obj)); } /** * Creates an unmodifiable {@link List} from a collection. */ public static <T> List<T> newReadOnlyList( final Collection<? extends T> c) { return Collections.unmodifiableList(newList(c)); } /** * 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; } /** * 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; } }