
数据处理中常见的需求是将列表中的元素按照某个共同属性进行分组,并重新组织成更具层次感的结构,例如父子关系。这在处理复杂数据集时尤为重要,能够提高数据的可读性和可维护性。groovy提供了一系列强大的集合操作方法,能够以简洁高效的方式完成此类任务。
假设我们有一个包含多个Map的列表,每个Map代表一个具有不同属性的数据项。我们的目标是根据其中一个特定属性(例如coverageType)将这些数据项进行分组,并将相同coverageType的数据项聚合到一个“子列表”中,最终形成一个包含“父”信息和“子”列表的结构。
考虑以下原始数据列表:
def fakeList = [
[coverageType: 'health', amount: 9, expireDate: 2020],
[coverageType: 'insurance', amount: 10, expireDate: 2020],
[coverageType: 'health', amount: 9, expireDate: 2021],
]我们期望的输出结构是这样的:
[
[
parent: 'health',
children: [
[
coverageType: 'health',
amount: '9',
expireDate: '2020'
],
[
coverageType: 'health',
amount: '9',
expireDate: '2021'
],
]
],
[
parent: 'insurance',
children: [
[
coverageType: 'insurance',
amount: '10',
expireDate: '2020'
]
]
],
]在尝试实现这种结构时,开发者可能会遇到一些挑战。例如,如果错误地使用putAll等方法,可能会导致数据被覆盖而非正确聚合,从而无法得到预期的父子结构。
Groovy为处理集合数据提供了非常便利且富有表达力的方法。解决上述问题的核心在于巧妙地结合使用groupBy和collect这两个方法。
通过先使用groupBy进行初步分组,然后使用collect将分组结果转换成我们所需的父子结构,可以高效地实现目标。
下面是实现上述数据重构的Groovy代码:
def fakeList = [
[coverageType: 'health', amount: 9, expireDate: 2020],
[coverageType: 'insurance', amount: 10, expireDate: 2020],
[coverageType: 'health', amount: 9, expireDate: 2021],
]
def groupedAndStructuredList = fakeList.groupBy { it.coverageType } // 第一步:按 coverageType 分组
.collect { coverageType, items -> // 第二步:遍历分组结果并重构
def parentChildMap = [:] // 创建一个Map来存储父子结构
parentChildMap.'parent' = coverageType // 设置父键
parentChildMap.'children' = items.collect { item -> // 为子列表进行转换
def childMap = [:] // 创建子项的Map
childMap.'coverageType' = item.coverageType
childMap.'amount' = item.amount as String // 将 amount 转换为字符串
childMap.'expireDate' = item.expireDate as String // 将 expireDate 转换为字符串
childMap // 返回子项Map
}
parentChildMap // 返回完整的父子结构Map
}
println groupedAndStructuredList代码解析:
[
health: [
[coverageType: 'health', amount: 9, expireDate: 2020],
[coverageType: 'health', amount: 9, expireDate: 2021]
],
insurance: [
[coverageType: 'insurance', amount: 10, expireDate: 2020]
]
]运行上述代码,将得到以下符合预期的结构化输出:
[
[parent:health, children:[
[coverageType:health, amount:9, expireDate:2020],
[coverageType:health, amount:9, expireDate:2021]
]],
[parent:insurance, children:[
[coverageType:insurance, amount:10, expireDate:2020]
]]
]如果需求是children列表中只包含值(例如,['health', '9', '2020']),而非键值对Map,只需修改内部的collect闭包即可:
def groupedAndStructuredListWithoutKeys = fakeList.groupBy { it.coverageType }
.collect { coverageType, items ->
def parentChildMap = [:]
parentChildMap.'parent' = coverageType
parentChildMap.'children' = items.collect { item ->
// 直接返回一个列表,包含所需的值
[item.coverageType, item.amount as String, item.expireDate as String]
}
parentChildMap
}
println groupedAndStructuredListWithoutKeys这将产生如下输出:
[
[parent:health, children:[
[health, 9, 2020],
[health, 9, 2021]
]],
[parent:insurance, children:[
[insurance, 10, 2020]
]]
]通过巧妙结合Groovy的groupBy和collect方法,我们可以高效、简洁地将一个包含Map的列表按照指定键进行分组,并重构为清晰的父子结构。这种模式在数据聚合、报表生成以及将扁平数据转换为层次结构数据等多种场景中都非常有用,是Groovy开发者工具箱中不可或缺的技能。
以上就是Groovy:使用groupBy方法将列表元素分组并构建父子结构的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号