Java Utililty Methods Map Put

List of utility methods to do Map Put

Description

The list of methods to do Map Put are organized into topic(s).

Method

MapputAllIfNew(Map dest, Map source)
Same as putall, except if the property already exists in the dest map, don't move it.
if (dest == null)
    return source;
if (source == null)
    return dest;
for (String key : source.keySet()) {
    if (dest.get(key) == null)
        dest.put(key, source.get(key));
return dest;
booleanputAllIfNotNull(final Map map, final Map m)
put All If Not Null
if ((map == null) || (m == null)) {
    return false;
map.putAll(m);
return true;
voidputAllNewInMap(Map original, Map newStuff)
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
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()) {
...
SetputAllNonNullValues(Map source, Map target)
Puts all entries from map source to map target whose values are non-null.
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());
...
MapputAllObjects(Map targetMap, Map sourceMap)
put All Objects
if (targetMap == null && sourceMap == null) {
    return new HashMap<T, U>();
if (targetMap == null) {
    return new HashMap<T, U>(sourceMap);
if (sourceMap == null) {
    return targetMap; 
...
voidputAllRecursively(Map destination, Map source)
Puts all the values in the given source Map into the destination Map , merging any Map values.
for (Map.Entry<String, Object> e : source.entrySet()) {
    String key = e.getKey();
    Object srcValue = e.getValue();
    Object dstValue = destination.get(key);
    if (srcValue instanceof Map && dstValue instanceof Map) {
        putAllRecursively((Map<String, Object>) dstValue, (Map<String, Object>) srcValue);
    } else {
        destination.put(key, srcValue);
...
voidputAllTransposed(Map> source_key_valueSet, Map output_value_key)
Takes a Map that goes from Object to Set, and fills in the transpose
for (Iterator<Object> it = source_key_valueSet.keySet().iterator(); it.hasNext();) {
    Object key = it.next();
    Set<Object> values = source_key_valueSet.get(key);
    for (Iterator<Object> it2 = values.iterator(); it2.hasNext();) {
        Object value = it2.next();
        output_value_key.put(value, key);
voidputAsCollection(K key, V value, Map map)
put As Collection
List<V> list = new ArrayList<V>();
if (map.containsKey(key)) {
    list = (List<V>) map.get(key);
list.add(value);
map.put(key, (L) list);
voidputAsStringIfNotNull(Map properties, String key, Object value)
put As String If Not Null
if (value == null)
    return;
properties.put(key, toString(value));
voidputAt(LinkedHashMap map, K key, V value, int pos)
put At
Iterator<Entry<K, V>> ei = map.entrySet().iterator();
LinkedHashMap<K, V> pre = new LinkedHashMap<>();
LinkedHashMap<K, V> post = new LinkedHashMap<>();
for (int i = 0; i < pos; i++) {
    if (!ei.hasNext())
        break;
    Entry<K, V> tmpE = ei.next();
    pre.put(tmpE.getKey(), tmpE.getValue());
...