Java List Clone cloneAdd(final List list, final E... element)

Here you can find the source of cloneAdd(final List list, final E... element)

Description

Clone a list and add N element to it.

License

MIT License

Parameter

Parameter Description
list List to clone
element vararg N element to add to the cloned list
E parametrized list type

Return

a new list with same content as the original list. element added

Declaration

@SafeVarargs
public static <E> List<E> cloneAdd(final List<E> list, final E... element) 

Method Source Code


//package com.java2s;
/*/* ww  w . jav a 2  s  .  co  m*/
 * 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 and add N element to it.
     *
     * @param list    List to clone
     * @param element vararg N element to add to the cloned list
     * @param <E>     parametrized list type
     * @return a new list with same content as the original list. element added
     */
    @SafeVarargs
    public static <E> List<E> cloneAdd(final List<E> list, final E... element) {
        final List<E> retList = clone(list);
        Collections.addAll(retList, element);
        return retList;
    }

    /**
     * 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. clone(List input)
  6. CloneList(final Collection list)
  7. cloneList(final List l, final int n)
  8. cloneList(List aList)
  9. cloneList(List l)

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