Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; public class Main { /** * Copies the contents of a Collection to a new, unmodifiable Collection. If the Collection is null, an empty * Collection will be returned. * <p> * This is especially useful for methods which take Collection parameters. Directly assigning such a Collection * to a member variable can allow external classes to modify internal state. Classes may have different state * depending on the size of a Collection; for example, a button might be disabled if the the Collection size is zero. * If an external object can change the Collection, the containing class would have no way have knowing a * modification occurred. * * @param <T> The type of element in the Collection. * @param collection The Collection to copy. * @return A new, unmodifiable Collection which contains all of the elements of the List parameter. Will never be * null. */ public static <T> Collection<T> copyToUnmodifiableCollection(Collection<T> collection) { if (collection != null) { Collection<T> collectionCopy = new ArrayList(collection); return Collections.unmodifiableCollection(collectionCopy); } else { return Collections.emptyList(); } } }