Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于Java/Grails中的元素从映射中提取/过滤数据_Java_Grails_Arraylist - Fatal编程技术网

基于Java/Grails中的元素从映射中提取/过滤数据

基于Java/Grails中的元素从映射中提取/过滤数据,java,grails,arraylist,Java,Grails,Arraylist,我有一张地图(见下面的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:

我有一张地图(见下面的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));
    }
列表结果=[]
for(对象y:GetUniqueValues(orginalList.dID)){
导出=新发票导出()
export.dID=y
结果

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]]

正如@michal szulc所说,您发布的输入列表实际上是一个
Map

[info:"D355",brand:"Opel",dID:7,price:30000]
由于数据来自数据库查询,很可能您有一个
列表
(地图列表)。如下所示:

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)

groupBy()

[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]]]
[['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]]
它很难阅读,但它是一个
地图
,其中的关键是
投标
地图
列表
,每个
地图
都属于具有相同
投标
的车辆。呸

最后,
collect()
构建一个新的
列表
of
Map
s,如下所示:

[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]]]
[['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]]

我如何提取代码请你举个例子。告诉我你的代码现在是什么样子(使用我的线索后),你有什么问题吗?我不明白,我有三个BWM与dID 3和梅赛德斯与dID 5和一个欧宝与dID 7,如何在代码中做到这一点?