Java Map Add addToMapIfNotNull(Map map, K key, V value)

Here you can find the source of addToMapIfNotNull(Map map, K key, V value)

Description

Adds a value V into a map under key K only if the value is not null.

License

Open Source License

Parameter

Parameter Description
map the map to add the entry under
key the key to add to the map
value the value to add to the map

Declaration

public static final <K, V> void addToMapIfNotNull(Map<K, V> map, K key, V value) 

Method Source Code


//package com.java2s;
/*/*  www  .ja  v a 2  s.c o m*/
 * EmailService - RESTful service that sends emails
 * Copyright (C) 2014  Terry Yiu
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Map;

public class Main {
    /**
     * Adds a value V into a map under key K only if the value is not null.
     * 
     * @param map
     *            the map to add the entry under
     * @param key
     *            the key to add to the map
     * @param value
     *            the value to add to the map
     */
    public static final <K, V> void addToMapIfNotNull(Map<K, V> map, K key, V value) {
        if (value == null) {
            return;
        }

        map.put(key, value);
    }
}

Related

  1. addToMap(Map dest, Map src)
  2. addToMap(Map aMap, T aKey, Float aValue)
  3. addToMap(Map map, T item)
  4. addToMap(String aDimensionCriteria, Map aDrilldownDimensions)
  5. addToMap(String key, Map map, String value)
  6. addToMapIfNotNull(Map map, String key, Object value)
  7. addToMapListUnique(Map> mapList, T key, List values)
  8. addToMapMap(Map> map, K1 key1, K2 key2, V value)
  9. addToMapMap(Map> map, T key, V val)