Groovy合并两个列表?
发布时间:2020-12-14 16:29:07 所属栏目:大数据 来源:网络整理
导读:我有两个清单: listA: [[Name: mr good,note: good,rating:9],[Name: mr bad,note: bad,rating:5]] listB: [[Name: mr good,score:77],score:12]] 我想得到这个 listC:[[Name: mr good,rating:9,rating:5,score:12]] 我怎么能这样做? 谢谢. 解决方法 收集l
我有两个清单:
listA: [[Name: mr good,note: good,rating:9],[Name: mr bad,note: bad,rating:5]] listB: [[Name: mr good,score:77],score:12]] 我想得到这个 listC: [[Name: mr good,rating:9,rating:5,score:12]] 我怎么能这样做? 谢谢. 解决方法
收集listA中的所有元素,并在listB中找到elementA equivilient.从listB中删除它,并返回组合元素.
如果我们说你的结构如上,我可能会这样做: def listC = listA.collect( { elementA -> elementB = listB.find { it.Name == elementA.Name } // Remove matched element from listB listB.remove(elementB) // if elementB == null,I use safe reference and elvis-operator // This map is the next element in the collect [ Name: it.Name,note: "${it.note} ${elementB?.note :? ''}",// Perhaps combine the two notes? rating: it.rating?:0 + elementB?.rating ?: 0,// Perhaps add the ratings? score: it.score?:0 + elementB?.score ?: 0 // Perhaps add the scores? ] // Combine elementA + elementB anyway you like } // Take unmatched elements in listB and add them to listC listC += listB (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |