I haven't added the getter and setter for simplicity. I'm facing a mapping issue here. I want to achieve this using java stream.
public class P{
private ArrayList<Lin> lin;
}
public class Lin{
private Oh oh;
private ArrayList<Pr> pr;
private ArrayList<Qty> qty;
}
public class Oh{
private int ohId;
private String size;
}
public class Pr{
private int prId;
private String sys;
}
public class Qty{
private int qtyId;
private String older;
}
With the above P class I want to convert a entity in the below structure,
public class OrderLi{
private int ohId;
private String size;
private ArrayList<Pr> pr;
private ArrayList<Qty> qty;
}
I want to convert this using java stream without adding repeated loops.
From what I understand, you want to convert from a P
to list of OrderLi
, here is my solution.
final List<OrderLi> orderLiList = p.getLin()
.stream()
.map(
lin -> new OrderLi(
Optional.ofNullable(lin.getOh()).map(Oh::getOhId).orElse(0),
Optional.ofNullable(lin.getOh()).map(Oh::getSize).orElse(null),
lin.getPr(),
lin.getQty()
)
)
.collect(Collectors.toList());