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

voidaddOne(Map map, K key)
Increment a Map of K->Integer for a given key.
if (map != null) {
    if (map.get(key) != null) {
        map.put(key, map.get(key) + 1);
    } else {
        map.put(key, 1l);
voidaddOne(Map map, T key)
given a map of ?
Integer count = map.get(key);
if (count == null)
    count = 0;
map.put(key, count + 1);
Map>addTo(Map> map, K key, T value)
Add a value to a map containing a list of values mapped to a key.
List<T> tl = map.get(key);
tl.add(value);
map.put(key, tl);
return map;
voidaddTo(Map map, N key, E element, Class collectionClass)
Adds element to the value-collection of key in map.
C collection = map.get(key);
if (collection == null) {
    try {
        collection = (C) collectionClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    map.put(key, collection);
...
TaddToAnd(T target, Map... items)
add To And
for (Map item : items)
    ((List) target.get("$and")).add(item);
return target;
ListaddToList(Map> map, X key, Y value)
add To List
List<Y> values;
if (map.containsKey(key)) {
    values = map.get(key);
    values.add(value);
} else {
    values = new ArrayList<Y>();
    values.add(value);
    map.put(key, values);
...
voidaddToListMap(Map map, Object key, Object value)
Add a value to a Map from keys to Set of values, creating the value list as needed.
if (map == null) {
    throw new IllegalArgumentException("invalid map");
if (key == null) {
    throw new IllegalArgumentException("invalid map key");
List valuesList = (List) map.get(key);
if (valuesList == null) {
...
booleanaddToListMap(Map> map, K key, V value)
Populated a (key,value) pair in a map of lists.
if (map == null) {
    return false;
List<V> list = map.get(key);
if (list == null) {
    list = new ArrayList<V>();
    map.put(key, list);
if (!list.contains(value)) {
    list.add(value);
    return true;
return false;
voidaddToListMap(Map> map, String key, String value)
Adds another element to the Map: The list of strings is enlarged
List<String> currentList;
if (map.containsKey(key)) {
    currentList = map.get(key);
} else {
    currentList = new ArrayList<String>();
currentList.add(value);
map.put(key, currentList);
...
Map>addToListMap(Map> map, T key, T value)
add To List Map
if (map == null)
    return null;
if (key == null || value == null)
    return map;
List<T> set = map.get(key);
if (set == null)
    set = new ArrayList<T>();
set.add(value);
...