我喜歡決議以下串列。
LUT = [
[branch: "test", name: 'a', image_name: 'abc'],
[branch: "test", name: 'b', image_name: 'abc'],
[branch: "test", name: 'c', image_name: 'abc'],
[branch: "test-1", name: 'd', image_name: 'abc'],
[branch: "test-1", name: 'e', image_name: 'abc'],
[branch: "test-2", name: 'f', image_name: 'abc'],
[branch: "test-2", name: 'g', image_name: 'abc'],
[branch: "test-2", name: 'h', image_name: 'abc'],
[branch: "test-3", name: 'i', image_name: 'abc'],
[branch: "test-3", name: 'j', image_name: 'abc'],
[branch: "test-4", name: 'k', image_name: 'abc'],
[branch: "test-5", name: 'l', image_name: 'abc'],
]
現在我要做的是:
result = [:]
for (map in LUT)
{
if (!result.containsKey(map['branch']))
{
println map['name'] // prints the unique name
result.put(map['branch'], map['name'])
}
}
這將導致串列中的 1 對 1 映射:
[test:a, test-1:d, test-2:f, test-3:i, test-4:k, test-5:l]
但我想要的是一個串列串列,如:
[test:[a:abc], test-1:[d:abc], test-2:[f:abc], test-3:[i:abc], test-4:[k:abc], test-5:[l:abc]]
誰能幫我解決這個問題,謝謝
uj5u.com熱心網友回復:
我不知道groovy,但我想我可以提供幫助。
而不是這個:
result = [:]
for (map in LUT)
{
if (!result.containsKey(map['branch']))
{
println map['name'] // prints the unique name
result.put(map['branch'], map['name'])
}
}
你需要這樣的東西:
result = [:]
for (map in LUT)
{
if (!result.containsKey(map['branch']))
{
println map['name'] // prints the unique name
newMap = [:]
newMap.put(map['name'], map['image_name'])
result.put(map['branch'], newMap)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510171.html
標籤:爪哇列表解析詹金斯时髦的
