如果提供此字符串:
[[name:11PUB2, carrier:AA, cost:326.99], [name:FRED, carrier:AS, cost:326.99], [name:11PUB2, carrier:UA, cost:326.99], [name:FRED, carrier:AA, cost:333.99], [name:11PUB2, carrier:UA, cost:334.99], [name:11PUB2, carrier:AA, cost:338.99], [name:11PUB2, carrier:WN, cost:339.99], [name:11PUB2, carrier:WN, cost:339.99], [name:11PUB2, carrier:WN, cost:343.99], [name:11PUB2, carrier:B6, cost:361.99], [name:11PUB2, carrier:WN, cost:409.99], [name:11PUB2, carrier:AA, cost:478.99], [name:11PUB2, carrier:SY, cost:498.99], [name:FRED, carrier:AS, cost:534.99], [name:11PUB2, carrier:AA, cost:573.99], [name:11PUB2, carrier:AA, cost:579.99], [name:11PUB2, carrier:AA, cost:579.99], [name:11PUB2, carrier:AA, cost:594.99], [name:11PUB2, carrier:AA, cost:598.99], [name:11PUB2, carrier:AA, cost:598.99], [name:11PUB2, carrier:AA, cost:598.99], [name:11PUB2, carrier:AA, cost:598.99], [name:11PUB2, carrier:UA, cost:600.99], [name:11PUB2, carrier:SY, cost:623.99], [name:11PUB2, carrier:0M, cost:664.99], [name:11PUB2, carrier:AS, cost:710.99], [name:11PUB2, carrier:AA, cost:720.99], [name:11PUB2, carrier:UA, cost:731.99], [name:11PUB2, carrier:UA, cost:745.99], [name:11PUB2, carrier:UA, cost:755.99], [name:11PUB2, carrier:UA, cost:755.99], [name:11PUB2, carrier:WN, cost:804.99], [name:11PUB2, carrier:AA, cost:816.99], [name:FRED, carrier:UA, cost:839.99], [name:11PUB2, carrier:B6, cost:843.99], [name:11PUB2, carrier:UA, cost:867.99], [name:11PUB2, carrier:UA, cost:889.99], [name:11PUB2, carrier:UA, cost:889.99], [name:11PUB2, carrier:UA, cost:948.99], [name:11PUB2, carrier:B6, cost:987.99], [name:11PUB2, carrier:UA, cost:1008.99], [name:11PUB2, carrier:UA, cost:1024.99], [name:FRED, carrier:UA, cost:1024.99], [name:11PUB2, carrier:WN, cost:1027.99]]
查找字符串中每个名称的载波总数的最简单方法是什么?最低的单位名称费用又如何呢?
答案 0 :(得分:2)
def rows= [["name":"11PUB2", "carrier":"AA", "cost":326.99], ["name":"FRED", "carrier":"AS", "cost":326.99], ["name":"11PUB2", "carrier":"UA", "cost":326.99]]
def ret = rows.groupBy{it.name}.collectEntries{
[
it.key,
[
count: it.value.size(),
minCost: it.value.sort{it.cost}[0],
]
]
}
println ret
答案 1 :(得分:0)
简单明了的东西
def rows= [[name:'11PUB2', carrier:'AA', cost:326.99], [name:'FRED', carrier:'AS', cost:326.99], [name:'11PUB2', carrier:'UA', cost:326.99], [name:'FRED', carrier:'AA', cost:333.99], [name:'11PUB2', carrier:'UA', cost:334.99], [name:'11PUB2', carrier:'AA', cost:338.99], [name:'11PUB2', carrier:'WN', cost:339.99], [name:'11PUB2', carrier:'WN', cost:339.99], [name:'11PUB2', carrier:'WN', cost:343.99], [name:'11PUB2', carrier:'B6', cost:361.99], [name:'11PUB2', carrier:'WN', cost:409.99], [name:'11PUB2', carrier:'AA', cost:478.99], [name:'11PUB2', carrier:'SY', cost:498.99], [name:'FRED', carrier:'AS', cost:534.99], [name:'11PUB2', carrier:'AA', cost:573.99], [name:'11PUB2', carrier:'AA', cost:579.99], [name:'11PUB2', carrier:'AA', cost:579.99], [name:'11PUB2', carrier:'AA', cost:594.99], [name:'11PUB2', carrier:'AA', cost:598.99], [name:'11PUB2', carrier:'AA', cost:598.99], [name:'11PUB2', carrier:'AA', cost:598.99], [name:'11PUB2', carrier:'AA', cost:598.99], [name:'11PUB2', carrier:'UA', cost:600.99], [name:'11PUB2', carrier:'SY', cost:623.99], [name:'11PUB2', carrier:'0M', cost:664.99], [name:'11PUB2', carrier:'AS', cost:710.99], [name:'11PUB2', carrier:'AA', cost:720.99], [name:'11PUB2', carrier:'UA', cost:731.99], [name:'11PUB2', carrier:'UA', cost:745.99], [name:'11PUB2', carrier:'UA', cost:755.99], [name:'11PUB2', carrier:'UA', cost:755.99], [name:'11PUB2', carrier:'WN', cost:804.99], [name:'11PUB2', carrier:'AA', cost:816.99], [name:'FRED', carrier:'UA', cost:839.99], [name:'11PUB2', carrier:'B6', cost:843.99], [name:'11PUB2', carrier:'UA', cost:867.99], [name:'11PUB2', carrier:'UA', cost:889.99], [name:'11PUB2', carrier:'UA', cost:889.99], [name:'11PUB2', carrier:'UA', cost:948.99], [name:'11PUB2', carrier:'B6', cost:987.99], [name:'11PUB2', carrier:'UA', cost:1008.99], [name:'11PUB2', carrier:'UA', cost:1024.99], [name:'FRED', carrier:'UA', cost:1024.99], [name:'11PUB2', carrier:'WN', cost:1027.99]]
def stats = rows.inject( [:].withDefault{ [ minCost:Double.MAX_VALUE, freqs:[:].withDefault{ 0 } ] } ){ res, it ->
res[ it.name ].freqs[ it.carrier ]++
res[ it.name ].minCost = Math.min res[ it.name ].minCost, it.cost
res
}
assert '[11PUB2:[minCost:326.99, freqs:[AA:13, UA:13, WN:6, B6:3, SY:2, 0M:1, AS:1]], FRED:[minCost:326.99, freqs:[AS:2, AA:1, UA:2]]]' == stats.toString()