Collections 使用新的Java8流将Map转换为List

Collections 使用新的Java8流将Map转换为List,collections,java-8,java-stream,Collections,Java 8,Java Stream,我需要使用Java8StreamsAPI进行转换的帮助 Map<String, List<Entry<Parameter, String>>> inputData 及 假设输入数据包含以下内容 {"ABC", {{Parameter.Foo, "hello"},{Parameter.Bar, "bye"} } {"DEF", {{Parameter.Baz, "hello1"},{Parameter.Foo, "bye1"} } 我希望测试列表包含 {

我需要使用Java8StreamsAPI进行转换的帮助

Map<String, List<Entry<Parameter, String>>> inputData

假设输入数据包含以下内容

{"ABC", {{Parameter.Foo, "hello"},{Parameter.Bar, "bye"} }
{"DEF", {{Parameter.Baz, "hello1"},{Parameter.Foo, "bye1"} }
我希望测试列表包含

{
   TestSession("ABC", Parameter.Foo, "hello"), 
   TestSession("ABC", Parameter.Bar, "bye"), 
   TestSession("DEF", Parameter.Baz, "hello1"),
   TestSession("DEF", Parameter.Foo, "bye1")
}

其思想是,每个TestSession都是使用来自inputData的键和来自列表中每个条目的条目来构造的。

假设TestSession类也相应地进行了调整,以包括一个参数字段,您可以执行以下操作:

List<TestSession> result = inputData.entrySet().stream()
    .collect(
      ArrayList::new,
      (list, e1) -> e1.getValue().forEach((e2) ->
        list.add(new TestSession(e1.getKey(), e2.getKey(), e2.getValue()))),
      ArrayList::addAll);
如a中所述,此问题可以通过和轻松解决:


你已经试过什么了?什么不起作用?你的问题可以很容易地用@Holger解决。今天早上我在句末看到了错误,我修正了它以改进问题,这就是否决票的原因吗?@johnco3:我没有否决票,所以我只能猜测。最有可能的情况是,选民认为你在自己解决问题上没有表现出足够的努力。@Holger谢谢你的意见,顺便说一句,我会在几分钟内尝试你建议的答案
{"ABC", {{Parameter.Foo, "hello"},{Parameter.Bar, "bye"} }
{"DEF", {{Parameter.Baz, "hello1"},{Parameter.Foo, "bye1"} }
{
   TestSession("ABC", Parameter.Foo, "hello"), 
   TestSession("ABC", Parameter.Bar, "bye"), 
   TestSession("DEF", Parameter.Baz, "hello1"),
   TestSession("DEF", Parameter.Foo, "bye1")
}
List<TestSession> result = inputData.entrySet().stream()
    .collect(
      ArrayList::new,
      (list, e1) -> e1.getValue().forEach((e2) ->
        list.add(new TestSession(e1.getKey(), e2.getKey(), e2.getValue()))),
      ArrayList::addAll);
List<TestSession> list = mapList.entrySet().stream()
    .flatMap(e1 -> e1.getValue().stream()
        .map(e2 -> new TestSession(e1.getKey(), e2.getKey(), e2.getValue())))
    .collect(Collectors.toList());