美文网首页
reduce by groovy

reduce by groovy

作者: 泠泉 | 来源:发表于2019-08-08 10:55 被阅读0次
 @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值应指定类型。

相关文章

网友评论

      本文标题:reduce by groovy

      本文链接:https://www.haomeiwen.com/subject/vhitjctx.html