Prepend an element to each list in a list
14:58 27 Jul 2024

Let's say we have the following base list:

[["foo"],["bar"]]

and the input string "a"

I want to prepend the input string to each list in the list, which results in:

[["a", "foo"], ["a", "bar"]]

The Javascript equivalent would be:

const base = [["foo"], ["bar"]]
const prefix = "a"
const prefixedBase = base.map(it => [prefix, ...it])

How can this be achieved using a similar idiomatic format with Java streams?

java java-stream spread-syntax