【问题标题】:how to add array in another array's element using groovy如何使用 groovy 在另一个数组的元素中添加数组
【发布时间】:2022-12-14 00:55:56
【问题描述】:

我有一个数组列表数组,我正在尝试使用 groovy 构建它,但没有得到预期的结果,我使用 putALL 方法添加一个数组,但它覆盖了以前的数组,

我试过这种方式,输出是这个[parent:health, children:[true]]

        import java.util.stream.Collectors

        def fakeList = [
                        [coverageType: 'health', amount: 9, expireDate: 2020], 
                        [coverageType: 'insurance', amount: 10, expireDate: 2020], 
                        [coverageType: 'health', amount: 9, expireDate: 2021],
                    ] 

        def groupList = [:]
        def list = fakeList
                .stream()
                .map { item -> 
                        def parentWithChilds = [
                                parent: item.coverageType,
                                child: groupList.stream()
                                .map { list -> list?.parent !== item.coverageType }
                                .collect(Collectors.toList())
                            ]
                            groupList.putAll(parentWithChilds)
                    }.collect(java.util.stream.Collectors.toList())

我的目标是,如果数组中有任何相同的 coverageType 值,我会将它们添加到子列表中,

这是我的预期输出:

          [     parent: 'health',
                children: [
                    [
                    coverageType: 'health',
                    amount:'9',
                    expireDate: '2020'                        
                    ],
                    [
                    coverageType: 'health',
                    amount:'9',
                    expireDate: '2021'                        
                    ],
                ]
            ],
            [
                parent: 'insurance',
                children: [
                    [
                    coverageType: 'insurance',
                    amount: '9',
                    expireDate: '2020'                        
                    ]
                ]
            ],

或者没有钥匙:

  [     parent: 'health',
                    children: [
                        [
                        'health',
                        '9',
                        '2020'                        
                        ],
                        [
                        'health',
                        '9',
                        '2021'                        
                        ],
                    ]
                ],
                [
                    parent: 'insurance',
                    children: [
                        [
                        'insurance',
                        '9',
                        '2020'                        
                        ]
                    ]
                ],

 

【问题讨论】:

    标签: java groovy


    【解决方案1】:

    鉴于这种:

    def fakeList = [
                    [coverageType: 'health', amount: 9, expireDate: 2020], 
                    [coverageType: 'insurance', amount: 10, expireDate: 2020], 
                    [coverageType: 'health', amount: 9, expireDate: 2021],
                ] 
    
    

    考虑一下:

    def groupList = fakeList.groupBy { it.coverageType }
                            .collect { coverageType, items ->
            def map = [:]
            map.'parent' = coverageType 
            map.'childs' = items.collect { item ->
                def childMap = [:]
                childMap.'coverage' = coverageType
                childMap.'amount' = item.amount as String
                childMap.'expireDate' = item.expireDate as String
                childMap
            }
            map
    }
    

    生成的地图是:

    [
    [parent:health, childs:[[coverage:health, amount:9, expireDate:2020], [coverage:health, amount:9, expireDate:2021]]],
    [parent:insurance, childs:[[coverage:insurance, amount:10, expireDate:2020]]]
    ]
    

    工作示例列出了here,它也转换为 JSON 以进行漂亮的打印。

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 2014-07-07
      • 2021-07-04
      • 1970-01-01
      • 2012-06-22
      • 2021-05-24
      相关资源
      最近更新 更多