Java List Copy copyAndRemove(final List in, final String remove)

Here you can find the source of copyAndRemove(final List in, final String remove)

Description

copy And Remove

License

Apache License

Declaration

private static List<String> copyAndRemove(final List<String> in, final String remove) 

Method Source Code


//package com.java2s;
/*//from   w ww. j  a  va 2 s  .  c  om
Copyright 2012 Mikael Gueck
    
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.
*/

import static java.util.Collections.*;
import java.util.*;

public class Main {
    private static List<String> copyAndRemove(final List<String> in, final String remove) {
        final List<String> list = new ArrayList<String>(in);
        list.remove(remove);
        return unmodifiableList(list);
    }

    static Map<String, List<String>> copyAndRemove(final Map<String, List<String>> in, final String key,
            final String value) {
        final Map<String, List<String>> ret = newMap();
        for (final Map.Entry<String, List<String>> e : in.entrySet()) {
            if (key.equals(e.getKey())) {
                ret.put(e.getKey(), copyAndRemove(e.getValue(), value));
            } else {
                ret.put(e.getKey(), copy(e.getValue()));
            }
        }
        return unmodifiableMap(ret);
    }

    static Map<String, List<String>> copyAndRemove(final Map<String, List<String>> in, final String key) {
        final Map<String, List<String>> ret = newMap();
        for (final Map.Entry<String, List<String>> e : in.entrySet()) {
            if (!key.equals(e.getKey())) {
                ret.put(e.getKey(), copy(e.getValue()));
            }
        }
        return unmodifiableMap(ret);
    }

    private static Map<String, List<String>> newMap() {
        return new HashMap<String, List<String>>();
    }

    private static List<String> copy(final Collection<String> in) {
        return unmodifiableList(new ArrayList<String>(in));
    }

    /** Deep copy, with immutable values. */
    static Map<String, List<String>> copy(final Map<String, List<String>> in) {
        final Map<String, List<String>> ret = newMap();
        for (final Map.Entry<String, List<String>> e : in.entrySet()) {
            ret.put(e.getKey(), unmodifiableList(new ArrayList<String>(e.getValue())));
        }
        return unmodifiableMap(ret);
    }
}

Related

  1. copy(List original)
  2. copy(List toCopy)
  3. copy(List toCopy)
  4. copy(List[] sourceLists, List[] destLists)
  5. copyAndClearList(List sourceList)
  6. copyAndSet(final Map> in, final String key, final String value)
  7. copyArrayToList(Object[] input, List output)
  8. copyFirst(List list, int count)
  9. copyIntArray(List li)