0

I have a YML file, which I parse to Map using yamlBeans library. I don't know how deep the nested map goes. for example:

  • key1:
    • key2: value1
    • key3:
      • key4: value2
      • key5: value3

I need to find a specific value in this map, update it, and write the map back to YML file (which I know how to do).

This is my code for updating the value, and it's working. However, this is only iterating twice through the nested map, and I need it to iterate it for as long as needed:

    static void updateYmlContent(Map<String, ?> ymlMap, String value, String... keys) {
    boolean found = false;
    for (Map.Entry entry : ymlMap.entrySet()) {
        if (entry.getKey().equals(keys[0])) {
            found = true;
            for (Map.Entry subEntry : ((Map<?, ?>) entry.getValue()).entrySet()) {
                if (subEntry.getKey().equals(keys[1])) {
                    subEntry.setValue(value);
                    break;
                } else {
                    throwKeyNotFoundException(keys[1]);
                }
            }
            break;
        }
    }
    if (!found) {
        throwKeyNotFoundException(keys[0]);
    }
}
barsi
  • 3
  • 1
  • 5
  • Is keys[] always two items and you need to find the parent/child relationship where it occurs, or is keys[] n items starting at the root and you need to follow that as a complete path to the leaf? – user1676075 Mar 14 '18 at 16:16
  • keys[] is n items (I know the complete path to the value) – barsi Mar 14 '18 at 16:19

3 Answers3

0

Use recursion and a depth counter to drop through each level of the map. I didn't compile this, so it probably needs a little tweaking, but here's the basic idea:

static void updateYmlContent(Map<String, ?> ymlMap, String value, String... keys) {
    int depth = 0;
    findAndReplaceContent(ymlMap, value, keys, depth);
}

static void findAndReplaceContent(Map map, .......) {
  if (map.containsKey(keys[depth]))
  {
    if (depth == keys.length - 1)
    {
      // found it
      map.put(keys[depth], value);
      // done
    }
    else
    {
      findAndReplaceContent(map.get(keys[depth]), value, keys, depth+1);
    }
  }
  else
  {
    // throw key not found
  }
}
user1676075
  • 3,056
  • 1
  • 19
  • 26
0

If ymlMap is mutable, it should be of type Map<String, Object> (ideally), i belive you have checked it already.

@SuppressWarnings("unchecked")
static void updateYmlContent(Map<String, ?> ymlMap, String value, String... keys)
{
  for (int i = 0, lastIndex = keys.length - 1; i <= lastIndex; i++)
  {
    String key = keys[i];
    Object v = ymlMap.get(key);
    if (v == null) // Assumed value is never null, if key exists
      throw new /* KeyNotFound */ RuntimeException("Key '" + key + "' not found");
    if (i < lastIndex)
      ymlMap = (Map<String, Object>) v;
    else
      ((Map<String, String>) ymlMap).put(key, value);
  }
}
Venkata Raju
  • 4,866
  • 3
  • 27
  • 36
0

You can do it via one for loop, please see the example:

private static void updateYmlContent(Map<String, Object> map, String newValue, String... keys) {
    for (int i = 0; i < keys.length; i++) {
        if (i + 1 == keys.length) {
            map.put(keys[i], newValue);
            return;
        }
        if (map.get(keys[i]) instanceof Map) {
            map = (Map<String, Object>) map.get(keys[i]);
        } else {
            throw new RuntimeException();
        }
    }
    throw new RuntimeException();
}

Also please see how it is used:

public static void  main(String[] keys) throws Exception {
    Map<String, Object> ymlMap = new HashMap<>();

    Map<Object, Object> nested1 = new HashMap<>();
    Map<Object, Object> nested2 = new HashMap<>();

    nested2.put("key3", "oldvalue1");
    nested2.put("key4", "oldvalue2");
    nested1.put("key2", nested2);
    ymlMap.put("key1", nested1);

    updateYmlContent(ymlMap, "new", "key1", "key2", "key3");

}
statut
  • 849
  • 5
  • 14