4

I have a nested HashMap with String keys that contains either List, Map, or String values. I would like to flatten them like the below.

Here is the data:

import java.util.*;
import java.util.stream.*;
public class MyClass {
    public static void main(String args[]) {
        Map<String, Object> dates = new HashMap<String, Object>() {{
            put("1999", new HashMap<String, Object>() {{
                put("3", Arrays.asList("23", "24", "25"));
                put("4", Arrays.asList("1", "2", "3"));
            }});
            put("2001", new HashMap<String, Object>() {{
                put("11", new HashMap<String, Object>() {{
                    put("7", Arrays.asList("23", "24", "25"));
                    put("9", Arrays.asList("1", "2", "3"));
                }});
                put("12", "45");
            }});
        }};
        System.out.println(dates);
    }
}

Map looks like:

{2001={11={7=[23, 24, 25], 9=[1, 2, 3]}, 12=45},
 1999={3=[23, 24, 25], 4=[1, 2, 3]}}

The flattening of map should look like this:

{2001.11.7.1=23, 2001.11.7.2=24, 2001.11.7.3=25, 2001.11.9.1=1, 2001.11.9.2=2, 
 2001.11.9.3=3, 2001.12=45, 1999.3.1=23, 1999.3.2=24, 1999.3.3=25,
 1999.4.1=1, 1999.4.2=2, 1999.4.3=3}

Note: the level of nested arrays or maps is unknown, it may go more than 2 levels.

3
  • If the amount of level is not known you should think about implementing a recursive approach. For doing this think about the recurring part and the ending condition of the recursion.
    – genius42
    Commented Jul 4, 2020 at 17:02
  • @genius42 Yes, I need something that checks until there are no maps or arrays and everything is flattened. The problem is I new to java and could not understand how to implement this, that's why I posted the question here. I really appreciate a solution with explanation. Commented Jul 4, 2020 at 17:04
  • I currently have no time to provide you with a solution. I can do that later or tomorrow. But to give you an idea: look at the different cases you have (Map or List) and you handle them. A map should be handled recursively where as a list should not.
    – genius42
    Commented Jul 4, 2020 at 17:10

3 Answers 3

2

You can use recursion to flatten the Map. Each time you encounter a Map, recurse by flattening that Map; when you encounter a List, iterate over it and add the index to the current key. A single value can be trivially set otherwise. See the below code in action here.

public static Map<String, Object> flatten(final Map<String, Object> map) {
    return flatten("", map, new HashMap<>());
    //use new TreeMap<>() to order map based on key
}

@SuppressWarnings("unchecked")//recursive helper method
private static Map<String, Object> flatten(final String key, final Map<String, Object> map,
        final Map<String, Object> result) {
    final Set<Map.Entry<String, Object>> entries = map.entrySet();
    if (!entries.isEmpty()) {
        for (final Map.Entry<String, Object> entry : entries) {
            //iterate over entries
            final String currKey = key + (key.isEmpty() ? "" : '.') + entry.getKey();
           //append current key to previous key, adding a dot if the previous key was not an empty String
            final Object value = entry.getValue();
            if (value instanceof Map) {//current value is a Map
                flatten(currKey, (Map<String, Object>) value, result);//flatten Map
            } else if (value instanceof List) {//current value is a List
                final List<Object> list = (List<Object>) value;
                for (int i = 0, size = list.size(); i < size; i++) {
                    result.put(currKey + '.' + (i + 1), list.get(i));
                }
                //iterate over the List and append the index to the current key when setting value
            } else {
                result.put(currKey, value);//set normal value
            }
        }
    }
    return result;
}
public static void main(final String[] args){
    final Map<String, Object> flattened = flatten(dates);
    System.out.println(flattened);
}
2
  • 1
    That was perfect can you add some explanation or comments. Thank you. Commented Jul 4, 2020 at 17:27
  • @SandeepKadapa No problem. Commented Jul 4, 2020 at 17:30
1

You can iterate over this map, and process each entry value, depending on its instance of: Map, List, or String. Since the level of nested arrays or maps is unknown, I have modified a little your code example and flat map format for clarity, also I used TreeMap instead of HashMap for entries ordering.

public static void main(String[] args) {
    TreeMap<String, Object> treeMap = new TreeMap<String, Object>() {{
        put("1999", new TreeMap<String, Object>() {{
            put("3", Arrays.asList("23", "24", "25"));
            put("4", Arrays.asList("1", "2", new TreeMap<String, Object>() {{
                put("10", "42");
            }}));
        }});
        put("2001", new TreeMap<String, Object>() {{
            put("11", new TreeMap<String, Object>() {{
                put("7", Arrays.asList("23", "24", "25"));
                put("9", Arrays.asList("1", "2", "3"));
            }});
            put("12", "45");
        }});
    }};

    TreeMap<String, String> flatMap = new TreeMap<>();
    processMap("", treeMap, flatMap);

    System.out.println(treeMap);
    System.out.println(flatMap);
}
private static void processMap(String prefix,
                               Map<String, Object> map,
                               Map<String, String> flatMap) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        processEntry(prefix, key, value, flatMap);
    }
}
private static void processList(String prefix,
                                List<Object> list,
                                Map<String, String> flatMap) {
    for (int i = 0; i < list.size(); i++) {
        String key = String.valueOf(i + 1);
        Object value = list.get(i);
        processEntry(prefix, key, value, flatMap);
    }
}
@SuppressWarnings("unchecked")
private static void processEntry(String prefix,
                                 String key,
                                 Object value,
                                 Map<String, String> flatMap) {
    if (value instanceof Map) {
        processMap(prefix + key + ".", (Map<String, Object>) value, flatMap);
    } else if (value instanceof List) {
        processList(prefix + key + ":", (List<Object>) value, flatMap);
    } else if (value instanceof String) {
        flatMap.put(prefix + key, (String) value);
    }
}

Sample map:

{1999={3=[23, 24, 25], 4=[1, 2, {10=42}]},
 2001={11={7=[23, 24, 25], 9=[1, 2, 3]}, 12=45}}

Flattened map:

{1999.3:1=23, 1999.3:2=24, 1999.3:3=25, 1999.4:1=1, 1999.4:2=2, 1999.4:3.10=42,
 2001.11.7:1=23, 2001.11.7:2=24, 2001.11.7:3=25,
 2001.11.9:1=1, 2001.11.9:2=2, 2001.11.9:3=3, 2001.12=45}

Opposite: Restoring a value tree from its flat map representation.

0
  private static Map<String, String> flatten(Map<String, Object> m) {
    var pairs = new ArrayList<String[]>();
    flatten(null, m, pairs);
    return pairs.stream().collect(Collectors.toMap(x -> x[0], x -> x[1]));
  }
  private static void flatten(String prefix, Map<String, Object> m, List<String[]> pairs) {
    for (var e : m.entrySet()) {
      var k = e.getKey();
      var o = m.get(k);
      if (o instanceof String s) {
        pairs.add(new String[]{makeKey(prefix, k), s});
      } else if (o instanceof List l) {
        IntStream.range(0, l.size())
            .mapToObj(i -> new String[]{
                makeKey(prefix, k + "." + (i +1)), 
                l.get(i).toString()
            })
            .forEach(p -> pairs.add(p));
      } else if (o instanceof Map nestedMap) {
        flatten(makeKey(prefix, k), nestedMap, pairs);
      }
    }
  }
  private static String makeKey(String prefix, String key) {
     return  String.join(".", prefix == null ? List.of(key) : List.of(prefix,key));
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.