Here you can find the source of cloneAdd(final List
Parameter | Description |
---|---|
list | List to clone |
element | vararg N element to add to the cloned list |
E | parametrized list type |
@SafeVarargs public static <E> List<E> cloneAdd(final List<E> list, final E... element)
//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; } }