Java Map Put putAllNewInMap(Map original, Map newStuff)

Here you can find the source of putAllNewInMap(Map original, Map newStuff)

Description

Puts the values of 2 maps together such that the values from the newStuff map are added to the original map only if they are not already in the the original

License

Educational Community License

Parameter

Parameter Description
T a parameter
S a parameter
original a parameter
newStuff a parameter

Declaration

public static <T, S> void putAllNewInMap(Map<T, S> original, Map<T, S> newStuff) 

Method Source Code

//package com.java2s;
/**// ww w. j a v  a2  s .  c om
 * $Id$
 * $URL$
 * EntityDataUtils.java - entity-broker - Aug 11, 2008 2:58:03 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ECL-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 java.util.*;
import java.util.Map.Entry;

public class Main {
    /**
     * Puts the values of 2 maps together such that the values from the newStuff map are added to the
     * original map only if they are not already in the the original
     * @param <T>
     * @param <S>
     * @param original
     * @param newStuff
     */
    public static <T, S> void putAllNewInMap(Map<T, S> original, Map<T, S> newStuff) {
        if (original == null) {
            throw new IllegalArgumentException("original map cannot be null");
        }
        if (newStuff != null) {
            if (original.isEmpty()) {
                original.putAll(newStuff);
            } else {
                for (Entry<T, S> entry : newStuff.entrySet()) {
                    if (original.containsKey(entry.getKey())) {
                        continue;
                    }
                    original.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
}

Related

  1. putAll(Map model, String prefix, Map subModel)
  2. putAll(Map map, String prefix, Map values)
  3. putAllIfAbsent(final Map target, final Map source)
  4. putAllIfNew(Map dest, Map source)
  5. putAllIfNotNull(final Map map, final Map m)
  6. putAllNonNullValues(Map source, Map target)
  7. putAllObjects(Map targetMap, Map sourceMap)
  8. putAllRecursively(Map destination, Map source)
  9. putAllTransposed( Map> source_key_valueSet, Map output_value_key)