我想通过其ProductType
及其相应的分组对订阅详细信息进行分组List<Long> planReferenceIds
密钥:planSubscription.productSubscription.productType
值:planSubscription.planReferenceId
我似乎不知道如何以“常规方式”执行此操作
private static Map<ProductType, List<Long>> buildPlanSubscriptionDetails(List<PlanSubscription> planSubscriptions) {
Map<ProductType, List<Long>> subscriptionDetails = [:]
subscriptionDetails = planSubscriptions.groupBy({it.productSubscription.productType })
return subscriptionDetails
编辑:
这是我想出的,但我猜不是它的Groovy方式
planSubscriptions.each { PlanSubscription planSubscription ->
List<Long> planRefIds = subscriptionDetails.get(planSubscription.productSubscription.productType)
if (planRefIds) {
planRefIds.add(planSubscription.planReferenceId)
} else {
subscriptionDetails.put(planSubscription.productSubscription.productType, [planSubscription.planReferenceId] as List)
}
}
先感谢您!
最佳答案
分组后应使用collectEntries
:
Map<ProductType, List<Long>> result = subscriptionDetails
.groupBy { it.productSubscription.productType }
.collectEntries { pt, sd -> [pt, sd*.planReferenceId]}