Java List Replace replace(List list, Object o, Object n)

Here you can find the source of replace(List list, Object o, Object n)

Description

Replaces the element o with n.

License

Apache License

Parameter

Parameter Description
list the list to search
o which element to replace
n the element to replace the existing element with

Return

o if it was replaced or null if it was not found.

Declaration

public static Object replace(List list, Object o, Object n) 

Method Source Code

//package com.java2s;
/*// www . j  av a  2  s  .co m
Copyright 1996-2010 Ariba, Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    
$Id: //ariba/platform/util/core/ariba/util/core/ListUtil.java#38 $
*/

import java.util.List;

public class Main {
    /**
    Replaces the element <b>o</b> with <b>n</b>.
        
    @param list the list to search
    @param o which element to replace
    @param n the element to replace the existing element with
        
    @return <b>o</b> if it was replaced or <b>null</b>
    if it was not found.
        
    @exception NullPointerException if <b>n</b> is
    <b>null</b>
    @aribaapi public
    */
    public static Object replace(List list, Object o, Object n) {
        int index = list.indexOf(o);
        if (index != -1) {
            return list.set(index, n);
        }
        return null;
    }
}

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, T element, List replacements)
  4. replace(List list, char target, char replacement)
  5. replace(List list, T from, T to)
  6. replace(String s, List replacementVars)