Java List Copy copyAndSet(final Map> in, final String key, final String value)

Here you can find the source of copyAndSet(final Map> in, final String key, final String value)

Description

copy And Set

License

Apache License

Declaration

static Map<String, List<String>> copyAndSet(final Map<String, List<String>> in, final String key,
            final String value) 

Method Source Code


//package com.java2s;
/*/*from  w  w w. ja v  a2  s  .c  o m*/
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 {
    static Map<String, List<String>> copyAndSet(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(), copy(e.getValue()));
            }
        }
        ret.put(key, newList(value));
        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);
    }

    private static List<String> newList(final String arg) {
        return unmodifiableList(singletonList(arg));
    }
}

Related

  1. copy(List toCopy)
  2. copy(List toCopy)
  3. copy(List[] sourceLists, List[] destLists)
  4. copyAndClearList(List sourceList)
  5. copyAndRemove(final List in, final String remove)
  6. copyArrayToList(Object[] input, List output)
  7. copyFirst(List list, int count)
  8. copyIntArray(List li)
  9. copyInto(List source, List dest)