I have an array of Node
which all have a list of Variable
:
Node[] arguments; // argument[i].vars() returns List<Variable>
I would like to create a list which contains all the variables. I do it like this today:
List<Variable> allVars = new ArrayList<>();
for (Node arg : arguments) {
allVars.addAll(arg.vars());
}
Can I do the same thing using streams?
I have tried this but it returns me a List<List<Variable>>
, whereas I would like a List<Variable>
with all the list's elements appended (using addAll
):
List<List<Variable>> vars = Arrays.asList(arguments).stream()
.map(Node::vars)
.collect(Collectors.toList());