public static Map assoc(
        final Map map, final Class<? extends Map> nodeClass, final Object[] path, final Object value
    ) {
        if (path == null || path.length == 0) {
            throw new IllegalArgumentException(PATH_MUST_BE_SPECIFIED);
        }
        if (value == null) {
            return map;
        }
        if (path.length == 1) {
            map.put(path[0], value);
            return map;
        }
        final Object[] pathToLastNode = Arrays.copyOfRange(path, 0, path.length - 1);
        final Object lastKey = path[path.length - 1];
        Map<Object, Object> intermediateMap = map;
        for (Object key : pathToLastNode) {
            final Object node = intermediateMap.computeIfAbsent(key, k -> ClassApi.newInstance(nodeClass));
            final Optional<Map> nodeMapOption = ClassApi.cast(node, Map.class);
            if (nodeMapOption.isPresent()) {
                intermediateMap = nodeMapOption.get();
            } else {
                throw new IllegalAccessError("Node with key '" + key + "' is not a map!");
            }
        }
        intermediateMap.put(lastKey, value);
        return map;
    }
public static Map assoc ( final Map map , final Class < ? extends Map > nodeClass , final Object [ ] path , final Object value ) { if ( path == null || path . length == 0 ) { throw new IllegalArgumentException ( PATH_MUST_BE_SPECIFIED ) ; } if ( value == null ) { return map ; } if ( path . length == 1 ) { map . put ( path [ 0 ] , value ) ; return map ; } final Object [ ] pathToLastNode = Arrays . copyOfRange ( path , 0 , path . length - 1 ) ; final Object lastKey = path [ path . length - 1 ] ; Map < Object , Object > intermediateMap = map ; for ( Object key : pathToLastNode ) { final Object node = intermediateMap . computeIfAbsent ( key , k -> ClassApi . newInstance ( nodeClass ) ) ; final Optional < Map > nodeMapOption = ClassApi . cast ( node , Map . class ) ; if ( nodeMapOption . isPresent ( ) ) { intermediateMap = nodeMapOption . get ( ) ; } else { throw new IllegalAccessError ( "Node with key '" + key + "' is not a map!" ) ; } } intermediateMap . put ( lastKey , value ) ; return map ; }
Associates new value in map placed at path. New nodes are created with nodeClass if needed.
@param map subject original map
@param nodeClass class for intermediate nodes
@param path nodes to walk in map path to place new value
@param value new value
@return original map