Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 BSI Business Systems Integration AG. * 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: * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.util.ArrayList; import java.util.Collection; public class Main { /** * Null safe creation of a {@link ArrayList} out of a given collection. The returned {@link ArrayList} is modifiable. * The result list is never null and does not contain any null elements. * * @param c * @return an {@link ArrayList} containing the given collection's elements. Never null */ public static <T> ArrayList<T> arrayListWithoutNullElements(Collection<? extends T> c) { if (c != null) { ArrayList<T> list = new ArrayList<T>(c.size()); for (T o : c) { if (o != null) { list.add(o); } } return list; } return emptyArrayList(); } public static <T> int size(Collection<T> list) { if (list == null) { return 0; } return list.size(); } /** * Returns a new empty {@link ArrayList}.<br> * This method differs to {@link Collections#emptyList()} in that way that the {@link ArrayList} returned by this * method can be modified hence is no shared instance. * * @return An empty but modifiable {@link ArrayList} with an initial capacity of <code>0</code>. */ public static <T> ArrayList<T> emptyArrayList() { return new ArrayList<T>(0); } }