Here you can find the source of replace(List extends T> list, T element, List
static <T> List<? extends T> replace(List<? extends T> list, T element, List<T> replacements)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.List; public class Main { static <T> List<? extends T> replace(List<? extends T> list, T element, List<T> replacements) { List<T> list2 = new ArrayList<T>(list.size() + replacements.size()); for (T node : list) if (node != element) list2.add(node);//from w w w. j a va2 s . com list2.addAll(replacements); return list2; } static <T> List<T> add(List<T> list, T element) { final ArrayList<T> result = new ArrayList<T>(list.size() + 2); result.addAll(list); result.add(element); return result; } }