Java Utililty Methods Map Add

List of utility methods to do Map Add

Description

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

Method

voidaddToListMap(Map> map, T key, V val)
add To List Map
if (!map.containsKey(key)) {
    List<V> list = new ArrayList<V>();
    list.add(val);
    map.put(key, list);
} else {
    map.get(key).add(val);
voidaddToListMap(Map> map, TKey key, TValue value)
Add the given value to the list of the corresponding key.
if (map.containsKey(key)) {
    map.get(key).add(value);
} else {
    List<TValue> list = new ArrayList<>();
    list.add(value);
    map.put(key, list);
intaddToMap(Map map, int initialKey, T value)
Adds a value to the given Map.
int i = initialKey;
while (true) {
    if (!map.containsKey(i)) {
        map.put(i, value);
        return i;
    i++;
voidaddToMap(Map data, K key, T value, Class clz)
add To Map
V values = data.get(key);
if (values == null) {
    try {
        values = (V) clz.newInstance();
    } catch (InstantiationException e) {
        throw new IllegalArgumentException("Failed to create collection class", e);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Failed to create collection class", e);
...
MapaddToMap(Map map, K key, V value)
Adds a key-value pair to a map unless the key or the value are null.
if (map == null)
    map = new HashMap<>();
if (key == null || value == null)
    return map;
map.put(key, value);
return map;
MapaddToMap(Map map, K key, V value)
add To Map
if (key == null)
    return map;
if (map == null)
    map = new HashMap<K, V>();
map.put(key, value);
return map;
voidaddToMap(Map> cfs, String[] qualSplits, String[] splitQual)
add To Map
if (cfs.get(qualSplits[0]) == null) {
    List<String> qual = new ArrayList<String>();
    qual.add(splitQual[0]);
    cfs.put(qualSplits[0], qual);
} else {
    List<String> list = cfs.get(qualSplits[0]);
    list.add(splitQual[0]);
voidaddToMap(Map dest, Map src)
add To Map
for (Map.Entry<?, ?> entry : src.entrySet()) {
    dest.put(entry.getKey().toString(), entry.getValue().toString());
voidaddToMap(Map aMap, T aKey, Float aValue)
add To Map
if (aMap != null) {
    if (!aMap.containsKey(aKey)) {
        aMap.put(aKey, aValue);
    } else {
        Float old = aMap.get(aKey);
        old = old + aValue;
        aMap.put(aKey, old);
voidaddToMap(Map map, T item)
add To Map
if (!map.containsKey(item)) {
    map.put(item, 1);
} else {
    int count = map.get(item);
    map.put(item, count + 1);