使用Stream展平嵌套的Hashmap

时间:2018-01-31 16:08:05

标签: java dictionary hashmap java-stream flatmap

我有一个嵌套的HashMap<String,Object>,我希望通过展平Hashmap来创建HashMap<String,String>。我尝试过Recursively Flatten values of nested maps in Java 8的解决方案。但我无法使用答案中提到的课程FlatMap

我也在问题本身尝试了解决方案,但我仍然缺少一些东西。然后我发现了一个类似的用例,并提出了以下解决方案。但似乎我缺少一些作为lambda函数flatMap的参数。

public static void main(String[] args) {
  Map<String,Object> stringObjectMap= new HashMap<String,Object>();
  stringObjectMap.put("key1","value1");
  stringObjectMap.put("key2","value2");
  Map<String,Object> innerStringObjectMap = new HashMap<>();
  innerStringObjectMap.put("i1key3","value3");
  innerStringObjectMap.put("i1key4","value4");
  innerStringObjectMap.put("i1key5","value5");
  stringObjectMap.put("map1",innerStringObjectMap);
  Map<String,Object> innerStringObjectMap2 = new HashMap<>();
  innerStringObjectMap.put("i2key6","value6");
  innerStringObjectMap2.put("i2key7","value7");
  innerStringObjectMap.put("i1map2",innerStringObjectMap2);

  Map<String,Object> collect =
        stringObjectMap.entrySet().stream()
                .map(x -> x.getValue())
                .flatMap(x -> x) //I aint sure what should be give here
                .distinct(); //there was a collect as List which i removed.

  //collect.forEach(x -> System.out.println(x));

}

展平嵌套地图有什么更好的解决方案?我不仅对价值感兴趣,还对地图中的键感兴趣。这就是为什么我决定将地图展平以获得另一张地图(我不确定这是否可能)的原因。

编辑 - 预期输出

key1 - value1
key2-value2
map1 =""  //this is something i will get later for my purpose
i1key3=value3
.
.
i1map2=""
.
.
i2key7=value7

2 个答案:

答案 0 :(得分:1)

我根据您的需要从mentioned answer修改了课程:

public class FlatMap {

  public static Stream<Map.Entry<?, ?>> flatten(Map.Entry<?, ?> e) {
    if (e.getValue() instanceof Map<?, ?>) {
      return Stream.concat(Stream.of(new AbstractMap.SimpleEntry<>(e.getKey(), "")),
                           ((Map<?, ?>) e.getValue()).entrySet().stream().flatMap(FlatMap::flatten));
    }

    return Stream.of(e);
  }
}

用法:

Map<?, ?> collect = stringObjectMap.entrySet()
                                   .stream()
                                   .flatMap(FlatMap::flatten)
                                   .collect(Collectors.toMap(
                                       Map.Entry::getKey,
                                       Map.Entry::getValue,
                                       (u, v) -> throw new IllegalStateException(String.format("Duplicate key %s", u)),
                                       LinkedHashMap::new));

<强>注意:
请务必使用提供的collect LinkedHashMap,否则订单将被搞砸。

答案 1 :(得分:0)

我使用了https://stackoverflow.com/a/48578105/5243291中的功能。但我以不同的方式使用了这个功能。

Map<Object, Object> collect = new HashMap<>();
stringObjectMap.entrySet()
        .stream()
        .flatMap(FlatMap::flatten).forEach(it -> {
          collect.put(it.getKey(), it.getValue());
});

再次发挥作用

public static Stream<Map.Entry<?, ?>> flatten(Map.Entry<?, ?> e) {
if (e.getValue() instanceof Map<?, ?>) {
  return Stream.concat(Stream.of(new AbstractMap.SimpleEntry<>(e.getKey(), "")),
          ((Map<?, ?>) e.getValue()).entrySet().stream().flatMap(FlatMap::flatten));
}

return Stream.of(e);
}