简体   繁体   English

展平 Java 8 多级地图后,我的预期结果为空

[英]After flattening a Java 8 multilevel map my expected result is empty

I have the below multilevel map:我有以下多级地图:

Map<String, List<Map<String, Map<String, Map<String, Map<String, String>>>>>> input =
                ImmutableMap.of("A",
                        ImmutableList.of(ImmutableMap.of("2",
                                ImmutableMap.of("3",
                                        ImmutableMap.of("4",
                                                ImmutableMap.of("5", "a"))))));

In short it'll be like总之它会像

 {
 "A":[{"2":{"3":{"4":{"5":"a"}}}}],
 "B":[{"2":{"3":{"4":{"5":"b"}}}}]
 }

My requirement is to construct a map of the form我的要求是构造一张表格的地图

{
 "A":"a", 
 "B":"b"
}

I tried the below code but for some reason myMap is always empty even though I'm populating it.我尝试了下面的代码,但由于某种原因,即使我正在填充它, myMap也始终为空。 What am I missing?我错过了什么?

Map<String, String> myMap = new HashMap<>();
input.entrySet()
                .stream()
                .map(l -> l.getValue().stream().map(m -> m.get(m.keySet().toArray()[0]))
                                .map(n -> n.get(n.keySet().toArray()[0]))
                                .map(o -> o.get(o.keySet().toArray()[0]))
                                .map(p -> myMap.put(l.getKey(), p.get(p.keySet().toArray()[0])))).collect(Collectors.toList());
System.out.println(myMap);

Here's what I get when I add two peek calls to your pipeline:这是我在管道中添加两个peek调用时得到的结果:

input.entrySet().stream()
  .peek(System.out::println)     //<- this
  .map(l -> ...)
  .peek(System.out::println)     //<- and this
  .collect(Collectors.toList());

output:输出:

A=[{2={3={4={5=a}}}}]
java.util.stream.ReferencePipeline$3@d041cf

If you notice the problem, you're collecting streams, and these streams don't get executed through a call to a terminal operation... When I try adding something like .count() to the inner stream, your expected output is produced:如果你注意到这个问题,你正在收集流,并且这些流不会通过调用终端操作来执行......当我尝试将.count()类的东西添加到内部流时,会产生预期的输出:

...
.map(l -> l.getValue().stream().map(m -> m.get(m.keySet().toArray()[0]))
        .map(n -> n.get(n.keySet().toArray()[0]))
        .map(o -> o.get(o.keySet().toArray()[0]))
        .map(p -> myMap.put(l.getKey(), p.get(p.keySet().toArray()[0])))
        .count()) //just an example
...

Now, I suppose you know that a terminal operation needs to be called for the intermediate ones to run.现在,我想您知道需要调用终端操作才能运行中间操作。

In a rather desperate attempt to simplify this code, as the stream seems to make it simply hard to read, I thought you might be interested in this, which assumes that no collection is empty in the tree but at least addrsses the record as one object, and not a collection of records (but I'm sure no code will look clean for that deep map of of maps).在简化此代码的相当绝望的尝试中,由于流似乎使其难以阅读,我认为您可能对此感兴趣,它假设树中没有集合是空的,但至少将记录添加为一个对象,而不是记录的集合(但我敢肯定,对于地图的深层地图,没有代码看起来很干净)。

String key = input.keySet().iterator().next();
String value = input.entrySet().iterator().next()
                .getValue().get(0)
                .values().iterator().next()
                .values().iterator().next()
                .values().iterator().next()
                .values().iterator().next();
myMap.put(key, value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM