Advertisement
dorucriv

Flatten Nested Map

Mar 30th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1.     public static Map<String, Object> flatten(Map<String, Object> map) {
  2.         Map<String, Object> result = new HashMap<>();
  3.         for (Map.Entry<String, Object> entry : map.entrySet()) {
  4.             Object value = entry.getValue();
  5.             if (value instanceof Map) {
  6.                 @SuppressWarnings("unchecked")
  7.                 Map<String, Object> nested = (Map<String, Object>) value;
  8.                 result.putAll(flatten(nested));
  9.             } else {
  10.                 result.put(entry.getKey(), value);
  11.             }
  12.         }
  13.         return result;
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement