public class A
{
private B[] b;
//getter setter
}
public class B
{
private String id;
//getter setter
}
I already got A object from stream as shown below but can't find way to complete this lambda to get List of ids which is inside B class.
Stream <String> lines = Files.lines(Paths.get("file.json"));
lines.map(x -> (A)gson.fromJson(x, type))...
You are looking for flatMap
here:
lines.map(x -> (A)gson.fromJson(x, type))
.flatMap(y -> Arrays.stream(y.getB()))
.map(B::getId)
.collect(Collectors.toSet()) // or any other terminal operation
You need to use flatMap
:
lines.map(x -> (A)gson.fromJson(x, type)).flatMap(a -> Arrays.stream(a.getB())
Now it's a Stream<B>
; you can map that to their Ids now
.map(B::getId)
and make a list out of this.
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With