Java Utililty Methods List Replace

List of utility methods to do List Replace

Description

The list of methods to do List Replace are organized into topic(s).

Method

voidreplace(final List list, final T newElement, final int index)
replace
if (!isIndexOutOfBounds(list, index)) {
    list.remove(index);
    list.add(index, newElement);
Stringreplace(final String source, final List search, final List repl)
replace
String result = source;
for (int i = 0; i < search.size(); ++i) {
    if (i < repl.size()) {
        result = result.replace(search.get(i), repl.get(i));
    } else {
        result = result.replace(search.get(i), repl.get(repl.size() - 1));
return result;
Objectreplace(List list, Object o, Object n)
Replaces the element o with n.
int index = list.indexOf(o);
if (index != -1) {
    return list.set(index, n);
return null;
Listreplace(List list, T element, List replacements)
replace
List<T> list2 = new ArrayList<T>(list.size() + replacements.size());
for (T node : list)
    if (node != element)
        list2.add(node);
list2.addAll(replacements);
return list2;
Listreplace(List list, char target, char replacement)
replace
ArrayList<String> result = new ArrayList<String>(list);
for (int i = 0; i < list.size(); i++) {
    result.set(i, result.get(i).replace(target, replacement));
return result;
Treplace(List list, T from, T to)
Replace an item with another item for the list
if (isEmpty(list))
    return null;
if (!list.contains(from))
    return null;
int index = list.indexOf(from);
list.set(index, to);
return to;
Stringreplace(String s, List replacementVars)
replace
for (String r : replacementVars) {
    s = s.replaceFirst("\\{\\}", r);
return s;
Stringreplace(String sourceStr, List indexList, String replaceStr)
replace
if (indexList.isEmpty()) {
    return sourceStr;
StringBuffer bf = new StringBuffer();
int t = 0;
int i = 0;
for (int[] ii : indexList) {
    bf.append(sourceStr.substring(t, ii[0]).trim()).append(replaceStr);
...
booleanreplaceAll(final List list, final T element0, final T element1)
replace All
return Collections.replaceAll(list, element0, element1);
booleanreplaceAllInList(T a, T b, List list)
replace All In List
final int max = list.size();
for (int i = 0; i < max; i++) {
    if (list.get(i) == a) {
        list.set(i, b);
return false;