Here you can find the source of addToMap(Map
Parameter | Description |
---|---|
map | Map to which the key-value pair will be added. |
key | Key in the map. |
value | Value added for the given key. |
K | Type of keys. |
V | Type of values. |
public static <K, V> Map<K, V> addToMap(Map<K, V> map, K key, V value)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { /**/* www .j a va 2 s . c o m*/ * Adds a key-value pair to a map unless the key or the value are null. * <p> * A new map is created if the provided map is null. * * @param map Map to which the key-value pair will be added. * @param key Key in the map. * @param value Value added for the given key. * @param <K> Type of keys. * @param <V> Type of values. * @return Map including added key-value pair. */ public static <K, V> Map<K, V> addToMap(Map<K, V> map, K key, V value) { if (map == null) map = new HashMap<>(); if (key == null || value == null) return map; map.put(key, value); return map; } }