Provides a shallow copy of the given collection. - Java Collection Framework

Java examples for Collection Framework:Collection

Description

Provides a shallow copy of the given collection.

Demo Code


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

import java.util.List;

public class Main {
    /**/*from  w w w  .  j a  v  a 2  s.  co  m*/
     * Provides a shallow copy of the given collection.
     * Removes all <code>null</code> items from <code>srcList</code>,
     *
     * @param srcList The source list. May be <code>null</code> and may contain <code>null</code> items.
     * @return A list with no <code>null</code> items. Never <code>null</code>.
     */
    public static <T> List<T> shallowCopyWithoutNulls(Collection<T> srcList) {
        if (srcList == null) {
            return new ArrayList<T>();
        }

        List<T> copy = new ArrayList<T>(srcList.size());
        for (T t : srcList) {
            if (t != null) {
                copy.add(t);
            }
        }

        return copy;
    }
}

Related Tutorials