Java Map Put put(Map map, String key, Object value)

Here you can find the source of put(Map map, String key, Object value)

Description

put

License

Apache License

Declaration

public static void put(Map<String, Object> map, String key, Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static void put(Map<String, Object> map, String key, Object value) {
        Map<String, Object> m = resolve(map, key);
        String k = resolveKey(key);
        m.put(k, value);/*from  ww  w  . jav a2 s .c  om*/
    }

    @SuppressWarnings("unchecked")
    private static Map<String, Object> resolve(Map<String, Object> map, String key) {
        String[] keys = key.split("\\.", 2);
        if (keys.length > 1) {
            if (!map.containsKey(keys[0]))
                map.put(keys[0], new HashMap<String, Object>());
            return resolve((Map<String, Object>) map.get(keys[0]), keys[1]);
        } else
            return map;
    }

    private static String resolveKey(String key) {
        int ixDot = key.lastIndexOf('.');
        if (ixDot > 0 && ixDot < key.length())
            return key.substring(ixDot + 1);
        return key;
    }
}

Related

  1. put(Map map, String k1, Object v1, String k2, Object v2)
  2. put(Map map, String keyValuePair)
  3. put(Map aMap, K aKey, V aValue)
  4. put(Map map, K key, V value)
  5. put(Map map, K key, V value)
  6. put(Map structure, String name, Object object)
  7. put(ThreadLocal> threadLocal, Object key, String value)
  8. put2DayOfWeekMaps(int pDayOfWeek, String pDayName)
  9. putAbsent(Map dest, Map src)