@Test
void mergeList(){
List l1 = [[id:1,a:1,b:2],[id:2,a:11,b:22]]
List l2 = [[id:1,c:3,d:3],[id:2,c:33,d:44]]
List l3 = [[id:1,e:5,f:6],[id:2,e:55,f:66]]
List ls= l1+=l2+=l3
println ls.inject([:]){acc,item->
acc.get(item.id as int, [:]).putAll(item)
acc
}.values()
}
acc.get(item.id
as int
, [:]).putAll(item)
这里需要注意弱类型会导致map有相同key值, 所以map的key值应指定类型。
网友评论