Java List Clone clone(final List list)

Here you can find the source of clone(final List list)

Description

Clone a list, not its content

License

MIT License

Parameter

Parameter Description
list the list to clone
E parametrized list type

Return

a new list with the same content as the original list

Declaration

public static <E> List<E> clone(final List<E> list) 

Method Source Code


//package com.java2s;
/*//from w ww  . j  av  a2  s.  c om
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */

import java.util.*;

public class Main {
    /**
     * Clone a list, not its content
     *
     * @param list the list to clone
     * @param <E>  parametrized list type
     * @return a new list with the same content as the original list
     */
    public static <E> List<E> clone(final List<E> list) {
        List<E> newInstance;
        try {
            newInstance = list.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            //fallback to ArrayList
            newInstance = new ArrayList<>();
        }
        newInstance.addAll(list);
        return newInstance;
    }
}

Related

  1. clone(List in)
  2. clone(List> doc)
  3. clone(List list)
  4. clone(List list)

  5. HOME | Copyright © www.java2s.com 2016