【问题标题】:Convert Map to Array Java将 Map 转换为数组 Java
【发布时间】:2017-03-23 16:03:23
【问题描述】:
public Map<String, List<Tuple4>> buildTestcases(ArrayList<Tuple4> list){
        Map<String, List<Tuple4>> map = new LinkedHashMap<String, List<Tuple4>>();


        for(Tuple4 element: list){

            String[] token = element.c.split("\\s");

              if (!map.containsKey(token[1])) {
                  map.put(token[1], new ArrayList<Tuple4>());
              }
            map.get(token[1]).add(element);
        }
        System.out.println(map);

        return map;
    }

 public Tuple4(String a, String b, String c, String d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

我正在为某些匹配的测试用例构建一个测试套件。现在我想将它转换为一个数组,因为我正在从中构造一个 dynamicTest:

 return Stream.of(<Array needed>).map(


            tuple -> DynamicTest.dynamicTest("Testcase: ", () -> { ... }

有没有办法将它转换为像Object[String][Tuple4]这样的数组

编辑:

好的,现在我有了这段代码:

`@TestFactory 公共流 dynamicTuple4TestsFromStream() 抛出 IOException{ 初始化();

    return map.entrySet().stream()
        .flatMap(entry -> 
                entry.getValue().stream()
                        .map(s -> new AbstractMap.SimpleEntry<>(entry.getKey(), s)))
        .forEach(e -> DynamicTest.dynamicTest("Testcase: " +e.getKey(), () -> {
            tester = new XQueryTester(e.getValue().a, e.getValue().b);
            if(e.getValue().c.contains("PAY")){
                Assert.assertTrue(tester.testBody(e.getValue().c,e.getValue().d)); 

            }

        })); }`

我得到了这个异常: incompatible types: void cannot be converted to java.util.stream.Stream&lt;org.junit.jupiter.api.DynamicTest&gt;

如何/为什么?

【问题讨论】:

  • Object[String][Tuple4]? -- 我不认为这是 Java 数组的规范。 Java 数组由整数索引。
  • 是的,抱歉我没说清楚,我的意思是在第一个索引中存储了字符串,在第二个索引中存储了我的元组。
  • forEach() 采用Consumer&lt;?&gt; -- 这是一个带有一个返回 void 的参数的函数。你给它传递了一个Function&lt;Entry,&lt;Stream&lt;DynamicTest&gt;&gt;,这就是它抱怨的原因。您可以通过编写更小的函数并将它们分配给变量来使这段代码更易于阅读,就像使用“extract-method”使方法更简单一样。

标签: java arrays hashmap toarray


【解决方案1】:

虽然您可以相当轻松地编写代码来将地图转换为数组,但您实际上并不需要为了获得测试流。

您只需要获取Map.Entry&lt;String,List&lt;Tuple&gt;&gt; 的流并将其展平为Map.Entry&lt;String,Tuple&gt; 的流

例如:

    Map<String, List<String>> map = new HashMap<>();
    map.put("Greeting", Arrays.asList("Hello", "World"));
    map.put("Farewell", Arrays.asList("Goodbye", "Suckers"));

    map.entrySet().stream()
            .flatMap(entry -> 
                    entry.getValue().stream()
                            .map(s -> new AbstractMap.SimpleEntry<>(entry.getKey(), s)))
            .forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));

...打印...

Greeting Hello
Greeting World
Farewell Goodbye
Farewell Suckers

当然如果你真的想要一个数组,你可以改变映射:

 .flatMap(entry -> entry.getValue().stream()
       .map( s -> new Object[] { entry.getKey(), entry.getValue() })

... 并将生成的数组流收集到数组中。但我不明白这一点。

(这将受益于功能分解重构,但它说明了这一点)

【讨论】:

  • 更新了我的问题,如果你能复习一下就好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
相关资源
最近更新 更多