我有一张地图见下面(orginalList),这来自一个oracle数据库,并包含元素
def orginalList =
[
[info:"F123",brand:"BMW",dID:3,price:40000],
[info:"F100",brand:"BMW",dID:3,price:40000],
[info:"F200",brand:"BMW",dID:3,price:40000],
[info:"C344",brand:"mercedes",dID:5,price:50000],
[info:"C354",brand:"mercedes",dID:5,price:50000],
[info:"D355",brand:"Opel",dID:7,price:30000]
]
我想过滤这个列表并将结果放入一个新的列表,如下面的列表(newList),你可以看到,宝马展示三次,梅赛德斯两次,欧宝一次,并具有相同的 dID ,所以我想在 dID 的基础上将宝马放在一排,而为欧宝提供两排梅赛德斯和一排。并计算总数,如下所示:
def newList =[['infos':'F123_F100_F200', 'brand':'BMW', 'dID':3, 'price':40000, 'total':3], ['infos':'C344_C354', 'brand':'mercedes', 'dID':5, 'price':50000, 'total':2], ['infos':'D355', 'brand':'Opel', 'dID':7, 'price':30000, 'total':1]]
我尝试了类似下面的东西,但是这并没有给出我想要的结果:
List<Export> result = []
for (Object y : GetUniqueValues(orginalList.dID)){
Export export = new InvoiceExport()
export.dID = y
result << export
}
public static ArrayList GetUniqueValues(Collection values)
{
return new ArrayList(new HashSet(values));
}
谁能提供帮助,请以身作则。感谢
答案 0 :(得分:0)
实际上
def orginalList= [info:"F123",brand:"BMW",dID:3,price:40000,
info:"F100",brand:"BMW",dID:3,price:40000,
info:"F200",brand:"BMW",dID:3,price:40000,
info:"C344",brand:"mercedes",dID:5,price:50000,
info:"C354",brand:"mercedes",dID:5,price:50000,
info:"D355",brand:"Opel",dID:7,price:30000]
不是列表,而是地图,因此您可以覆盖每个键六次。所以实际上你只有:
orginalList= [
info:"D355",brand:"Opel",dID:7,price:30000]
请根据此线索修复您的代码......
并记住你总是可以使用f.e。:
def orginalList= [[info:"F123",brand:"BMW",dID:3,price:40000],
[info:"F100",brand:"BMW",dID:3,price:40000]]
答案 1 :(得分:0)
正如@ michal-szulc所说,你发布的输入列表实际上是Map
。如果你要在Groovy中评估originalList
,你会得到:
[info:"D355",brand:"Opel",dID:7,price:30000]
由于数据来自数据库查询,因此您很可能拥有List<Map<String, Object>>
(地图列表)。像这样:
def orginalList =
[
[info:"F123",brand:"BMW",dID:3,price:40000],
[info:"F100",brand:"BMW",dID:3,price:40000],
[info:"F200",brand:"BMW",dID:3,price:40000],
[info:"C344",brand:"mercedes",dID:5,price:50000],
[info:"C354",brand:"mercedes",dID:5,price:50000],
[info:"D355",brand:"Opel",dID:7,price:30000]
]
那是有道理的。如果是这种情况,您可以使用groupBy(Closure)
和collect(Closure)
来实现您的目标:
orginalList.groupBy { it.dID }.collect { dID, maps ->
def head = maps.head()
[
infos: maps*.info.join('_'),
brand: head.brand,
dID: head.dID,
price: head.price,
total: maps.size()
]
}
groupBy()
Map
分组dID
按Map<Integer, List<Map<String, Object>>>
生成[3:[['info':'F123', 'brand':'BMW', 'dID':3, 'price':40000], ['info':'F100', 'brand':'BMW', 'dID':3, 'price':40000], ['info':'F200', 'brand':'BMW', 'dID':3, 'price':40000]], 5:[['info':'C344', 'brand':'mercedes', 'dID':5, 'price':50000], ['info':'C354', 'brand':'mercedes', 'dID':5, 'price':50000]], 7:[['info':'D355', 'brand':'Opel', 'dID':7, 'price':30000]]]
,如下所示:
Map
很难阅读,但它是bID
,其中关键是List
,Map
的值为Map
,每个bID
都有关联到具有相同collect()
的车辆。呼!
最后,List
构建Map
个[['infos':'F123_F100_F200', 'brand':'BMW', 'dID':3, 'price':40000, 'total':3], ['infos':'C344_C354', 'brand':'mercedes', 'dID':5, 'price':50000, 'total':2], ['infos':'D355', 'brand':'Opel', 'dID':7, 'price':30000, 'total':1]]
个std::map<std::string, int, std::less<>> m;
// ~~~~~~~~~~^
,如下所示:
std::map<K,V>