You asked:
is there a clean way to achieve this using Java Streams?
Yes. Indeed, you can process your inputs, filter out the unwanted parts, and produce a finished list, all using streams.
First define some example data, a list of inputs.
Generate a stream from that list. This stream has 3 elements, given our example data.
Call Stream#flatMap to return a new stream whose elements are made from pieces of the original stream’s elements. The new stream has 9 elements, produced by breaking each of the 3 original elements into 3 parts each (3 * 3 = 9).
Filter the newer longer stream for elements whose text starts with our targeted y= prefix.
Transform each of the string elements that passed our filter’s predicate test. Use simple string manipulation to replace "y=" with an empty string. So a value such as y=bb becomes bb.
Collect transformed strings by gathering into a new List object.
List < String > inputs =
List.of(
"x=aa,y=bb,y=cc" ,
"y=dd,z=ee,w=ff" ,
"w=gg,w=hh,w=ii"
);
List < String > results =
inputs
.stream() // Generate stream whose elements are each of the list’s elements.
.flatMap( input -> Arrays.stream( input.split( "," ) ) ) // Break each element into parts, feeding each part into a new longer stream.
.filter( pairing -> pairing.startsWith( "y=" ) ) // Filter out any strings not beginning with `y=`.
.map( pairing -> pairing.replace( "y=" , "" ) ) // A value such as `y=bb` becomes `bb`.
.toList(); // Collect transformed strings into a new list.
When run.
results.toString() = [bb, cc, dd]
To put results in sorted order, add a call to .sorted() before the toList().