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?
Best Answer
The idiomatic solution using Java streams I found for this is as follows:
While not as concise as Javascript, it does the job without much code. However, it's important to note that using Java streams requires an important trade-off between conciseness and readability.
For the sake of completeness, the equivalent code without Java streams would be:
EDIT: Based on other answers and comments, if the base list is mutable,
addFirst
oradd(0, prefix)
can also be used as a very concise solution: