Java Map Put putAllNonNullValues(Map source, Map target)

Here you can find the source of putAllNonNullValues(Map source, Map target)

Description

Puts all entries from map source to map target whose values are non-null.

License

Apache License

Parameter

Parameter Description
source Map providing the entries
target Map getting all entries

Return

A set of keys that contained null values and therefor were not put to target

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Set putAllNonNullValues(Map source, Map target) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * /*  ww w  . ja v a  2 s  . co m*/
 * 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 java.util.HashSet;
import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class Main {
    /**
     * Puts all entries from map source to map target whose values are non-null.
     * This can be used to fill ConcurrentHashMaps that do not take null values.
     * @param source Map providing the entries
     * @param target Map getting all entries
     * @return A set of keys that contained null values and therefor were not put to target
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Set putAllNonNullValues(Map source, Map target) {

        Iterator entries = source.entrySet().iterator();
        Set nullKeys = new HashSet();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            if (entry.getValue() != null) {
                target.put(entry.getKey(), entry.getValue());
            } else {
                nullKeys.add(entry.getKey());
            }
        }
        return nullKeys;

    }
}

Related

  1. putAll(Map map, String prefix, Map values)
  2. putAllIfAbsent(final Map target, final Map source)
  3. putAllIfNew(Map dest, Map source)
  4. putAllIfNotNull(final Map map, final Map m)
  5. putAllNewInMap(Map original, Map newStuff)
  6. putAllObjects(Map targetMap, Map sourceMap)
  7. putAllRecursively(Map destination, Map source)
  8. putAllTransposed( Map> source_key_valueSet, Map output_value_key)
  9. putAsCollection(K key, V value, Map map)