Java List Replace replace(List list, T from, T to)

Here you can find the source of replace(List list, T from, T to)

Description

Replace an item with another item for the list

License

Apache License

Parameter

Parameter Description
list a parameter
from a parameter
to a parameter

Declaration

public static <T> T replace(List<T> list, T from, T to) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.List;
import java.util.Map;

public class Main {
    /*****//from www  .j ava 2  s  .c  o  m
     * Replace an item with another item for the list
     * 
     * @param list
     * @param from
     * @param to
     * @return
     */
    public static <T> T replace(List<T> list, T from, T to) {
        if (isEmpty(list))
            return null;
        if (!list.contains(from))
            return null;

        int index = list.indexOf(from);
        list.set(index, to);

        return to;
    }

    public static boolean isEmpty(Iterable<?> i) {
        if (i instanceof Collection)
            return ((Collection<?>) i).isEmpty();
        return i == null || !i.iterator().hasNext();
    }

    public static boolean isEmpty(Map<?, ?> p_oCol) {
        return p_oCol == null || p_oCol.isEmpty();
    }
}

Related

  1. replace(final List list, final T newElement, final int index)
  2. replace(final String source, final List search, final List repl)
  3. replace(List list, Object o, Object n)
  4. replace(List list, T element, List replacements)
  5. replace(List list, char target, char replacement)
  6. replace(String s, List replacementVars)
  7. replace(String sourceStr, List indexList, String replaceStr)
  8. replaceAll(final List list, final T element0, final T element1)
  9. replaceAllInList(T a, T b, List list)