Given what you have orders: List<Order>
, you can first place flatMap
orders for pairs Order
and the name of the element (so that each Order
can occur several times if it has more than one Item
), and then group these pairs by the name of the element, using groupBy
, taking orders from pairs into groups:
val result = orders
.flatMap { o -> o.items.map { i -> o to i.name } }
.groupBy({ (_, name) -> name }, valueTransform = { (o, _) -> o })
groupBy
{ (_, name) -> name }
- , , { (o, _) -> o }
, .
(runnable demo )
Order
, Item
, distinct
: .flatMap { o -> o.items.distinct().map { i -> ... } }