2

I have a nested HashMap in this form:

{key1=val1, key2=val2, 
    key3=[
            {key4=val4, key5=val5}, 
            {key6=val6, key7=val7} 
        ]
}

I now want to flatten that map, so that all entries are on the same level:

{key1=val1, key2=val2, key4=val4, key5=val5,key6=val6, key7=val7}

When I try

map.values().forEach(map.get("key3")::addAll);

as described in this post, I get the following error:

invalid method reference
  cannot find symbol
    symbol:   method addAll(T)
    location: class Object
  where T is a type-variable:
    T extends Object declared in interface Iterable

Is there any generic way to flatten any given Map?

11
  • 1
    a recursive function with an instanceOf check against Map, I assume
    – Eugene
    Jan 14, 2019 at 14:52
  • 2
    you can try to use flatMap() from Java Stream API
    – i.merkurev
    Jan 14, 2019 at 14:55
  • 3
    map.get("key3")::addAll makes no sense since you seem to have a Map<String, Object>.
    – f1sh
    Jan 14, 2019 at 14:57
  • 4
    I would be really glad if you could share the type of your Map where the code that you've shared works.
    – Naman
    Jan 14, 2019 at 14:58
  • 4
    So what is the type of your Map? It can't be Map<String, String> if some of the entries are Entry<String, Map<String, String>>. You need to include the code for us to understand what you actually have.
    – jbx
    Jan 14, 2019 at 15:00

3 Answers 3

8

Not sure if I understood the question correctly, but something like this might work. Haven't checked all the syntax yet, so there might be some mistake somewhere.

Stream<Map.Entry<String, String>> flatten(Map<String, Object> map) {
  return map.entrySet()
            .stream()
            .flatMap(this::extractValue);         
}

Stream<Map.Entry<String, String>> extractValue(Map.Entry<String, Object> entry) {
   if (entry.getValue() instanceof String) {
      return Stream.of(new AbstractMap.SimpleEntry(entry.getKey(), (String) entry.getValue()));
   } else if (entry.getValue() instanceof Map) {
      return flatten((Map<String, Object>) entry.getValue());
   }
}

Then you could do:

Map<String, String> flattenedMap = flatten(yourmap)
   .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
2
  • the extractValue method won't compile, a return is missing in case both ifs fail
    – Chirlo
    Jan 30, 2020 at 9:41
  • @Chirlo yes because i don't really know what the OP wants to do in that case
    – jbx
    Jan 31, 2020 at 10:40
1

You can make use of a recursive helper method:

static void forEachValue(Map<String, Object> source, BiConsumer<? super String, ? super Object> action) {
    for (final Map.Entry<String, Object> entry : source.entrySet()) {
        if (entry.getValue() instanceof Map) {
            forEachValue((Map<String, Object>) entry.getValue(), action);
        } else {
            action.accept(entry.getKey(), entry.getValue());
        }
    }
}

Which then can be called like this:

Map<String, Object> map = ...;

Map<String, Object> flattened = new HashMap<>();
forEachValue(map, map::put);

I've used this approach with the BiConsumer to not limit the method to only flatten the nested map into another map, but the caller may decide himself what he wants to do with every key-value pair.

0

You should try this:

Map<String, Object> flatenedMap = new HashMap<>();

    map.forEach((key, value) -> {
      if(value instanceof Map) {
        flatenedMap.putAll((Map) value);
      } else {
        flatenedMap.put(key, value);
      }
    });

If you have more than one level of nesting you can use recursive alg.

static Map<String, Object> flatMap(Map<String, Object> map) {
    Map<String, Object> flatenedMap = new HashMap<>();
    map.forEach((key, value) -> {
      if(value instanceof Map) {
        flatenedMap.putAll(flatMap((Map) value));
      } else {
        flatenedMap.put(key, value);
      }
    });

    return flatenedMap;
  }
3
  • and what do you do if there are more than just 2 nested layers of maps?
    – Lino
    Jan 14, 2019 at 15:03
  • He wrote he has nested HashMap, if he has more than one layer of nesting then best would be to use some recursive alg
    – HPCS
    Jan 14, 2019 at 15:04
  • 1
    @Lino indeed, my bad.
    – Holger
    Jan 14, 2019 at 15:06

Your Answer

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

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