集册 Java实例教程 返回一个新的ArrayList,其内容与source相同,但source为nil或空的情况除外,在这种情况下,将返回source本身。

返回一个新的ArrayList,其内容与source相同,但source为nil或空的情况除外,在这种情况下,将返回source本身。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

454
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回一个新的ArrayList,其内容与source相同,但source为nil或空的情况除外,在这种情况下,将返回source本身。

/*******************************************************************************

 * Copyright (c) 2010 SAP 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:

 *    Emil Simeonov - initial API and implementation.

 *    Dimitar Donchev - initial API and implementation.

 *    Dimitar Tenev - initial API and implementation.

 *    Nevena Manova - initial API and implementation.

 *    Georgi Konstantinov - initial API and implementation.

 *    Jakob Spies - initial API and implementation.

 *******************************************************************************/

//package com.nowjava;
/**
n o w j a   v  a . c o m - 时  代  Java 提供 
**/

import java.util.ArrayList;

import java.util.Collection;


public class Main {

    public static void main(String[] argv) {

        Collection source = java.util.Arrays.asList("asdf", "nowjava.com");

        System.out.println(duplicate(source));

    }


    /**

     * Returns a new {@link ArrayList} with the same contents as <code>source</code>,

     * except for the case that <code>source</code> is nil or empty, in which <code>source</code>

     * itself is returned.

     * @param <T> element type

     * @param source the {@link Collection} to clone

     * @return a new {@link ArrayList} with the same contents as <code>source</code>,

     *    except for the case that <code>source</code> is nil or empty, in which <code>source</code>

     *    itself is returned

     */

    public static <T> Collection<T> duplicate(Collection<T> source) {

        final Collection&
展开阅读全文