Java 如何为列表中的数组写入lambda

Java 如何为列表中的数组写入lambda,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我已经从如下所示的流中获得了一个对象,但找不到完成这个lambda的方法来获取B类中的ID列表 public class A { private B[] b; //getter setter } public class B { private String id; //getter setter } streamlines=Files.lines(path.get(“file.json”); lines.map(x->(A)gson.fromJson(x,t

我已经从如下所示的流中获得了一个对象,但找不到完成这个lambda的方法来获取B类中的ID列表

public class A
{
    private B[] b;
    //getter setter
}

public class B
{
    private String id;
    //getter setter
}
streamlines=Files.lines(path.get(“file.json”);
lines.map(x->(A)gson.fromJson(x,type))。。。

您需要使用
flatMap

Stream <String> lines = Files.lines(Paths.get("file.json"));
lines.map(x -> (A)gson.fromJson(x, type))...
现在它是一个
;你现在可以把它映射到他们的ID上

 lines.map(x -> (A)gson.fromJson(x, type)).flatMap(a -> Arrays.stream(a.getB())
把这些列出来

    .map(B::getId)

您正在此处查找
flatMap

    .collect(Collectors.toList());

OP想要一个带有B的Id
List bId lines.map(x->(a)gson.fromJson(x,type)).flatMap(a->array.stream(a.getB()).map(B::getId.collector(Collectors.toList());
@DavidPérezCabrera你说得对。我更新了答案。
 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