-1
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;
    }

I am constructing a Testsuite for certain matching Testcases. Now I want to convert it to an Array since I'm constructing a dynamicTest from it:

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


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

Is there any way to convert it to an Array like Object[String][Tuple4]

Edit:

Okay, now I have this piece of code:

`@TestFactory public Stream dynamicTuple4TestsFromStream() throws IOException{ initialize();

    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)); 

            }

        })); }`

and im getting this exception: incompatible types: void cannot be converted to java.util.stream.Stream<org.junit.jupiter.api.DynamicTest>

How/Why?

Hendrik
  • 310
  • 6
  • 19
  • `Object[String][Tuple4]`? -- I don't recognise that as a spec for a Java array. Java arrays are indexed by ints. – slim Mar 23 '17 at 16:08
  • Yeah, sorry I wasn't clear there, i meant in the first index there are the strings stored and in the second my tuples. – Hendrik Mar 23 '17 at 16:10
  • `forEach()` takes a `Consumer>` -- that is a function with one parameter that returns void. You have passed it a `Function>`, which is why it's complaining. You can make this code easier to read by writing smaller functions and assigning them to variables, just as you make methods simpler with "extract-method". – slim Mar 24 '17 at 09:59

1 Answers1

0

While you could fairly easily write code to transform your map into an array, you don't really need to in order to get a stream of tests.

You just need get stream of Map.Entry<String,List<Tuple>> and flatten it into a stream of Map.Entry<String,Tuple>

For example:

    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()));

... prints ...

Greeting Hello
Greeting World
Farewell Goodbye
Farewell Suckers

Of course if you really want an array, you can change the mapping:

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

... and collect the resulting stream of arrays into an array-of-arrays. But I don't see the point.

(This would benefit from a functional-decomposition refactor, but it illustrates the point)

slim
  • 40,215
  • 13
  • 94
  • 127