2017-04-22 43 views
1

我有以下列表和代碼從名單越來越沒有重複(Groovy中)

def myList = [[id:8100-04, name:AAA, code:2281], 
      [id:8100-05, name:BBB, code:2102], 
      [id:8100-06, name:CCC, code:6089], 
      [id:8100-07, name:CCC, code:6089], 
      [id:8100-08, name:CCC, code:6089]] 
//list is retrived but looks something like the above 

def newList = myList.findAll { 
      (it.get("Name").equals("AAA") || 
       it.get("Name").equals("BBB") || 
       it.get("Name").equals("AFBO") || 
       it.get("Name").equals("CCC")) } 
def filteredListData = newList.collect { getListData(it.get("Id"), it.get("Name"), 
              it.get("Code")) } 

我想找回我的所有newList的,如果有重複的,然後檢索與最低ID索引的條目。

所以從myList中我期待的結果是:

[[id:8100-04, name:AAA, code:2281], 
[id:8100-05, name:BBB, code:2102], 
[id:8100-06, name:CCC, code:6089]] 
+0

JCK,希望複製應根據'名稱和code'來識別。請檢查答案。 – Rao

回答

0

你幾乎沒有。

def myList = [ 
       [id:"8100-04", name:"AAA", code:2281], 
       [id:"8100-05", name:"BBB", code:2102], 
       [id:"8100-06", name:"CCC", code:6089], 
       [id:"8100-07", name:"CCC", code:6089], 
       [id:"8100-08", name:"CCC", code:6089] 
      ] 

def newList = myList.findAll { 
       it.name in ["AAA", "BBB", "AFBO", "CCC"] 
       } 

def map = [:] 

newList.each{ 
    if(!map.get(it.name)) 
     map.put(it.name, it) 

}    

println map*.value 

輸出:

[ 
[id:8100-04, name:AAA, code:2281], 
[id:8100-05, name:BBB, code:2102], 
[id:8100-06, name:CCC, code:6089] 
] 
+1

如果原始列表按照id排序,則工作正常。否則,你必須先做一個排序,或者看看我的解決方案。 –

0

這裏是一個更 「功能樣式」 的解決方案。我們按名稱分組,這給出了每個名稱的列表。然後我們對這些列表中的每一個進行排序並採取其第一個元素

def myList = [[id:'8100-04', name:'AAA', code:2281], 
      [id:'8100-05', name:'BBB', code:2102], 
      [id:'8100-07', name:'CCC', code:6089], 
      [id:'8100-06', name:'CCC', code:6089], 
      [id:'8100-08', name:'CCC', code:6089]] 

def newList = myList.findAll { 
       it.name in ["AAA", "BBB", "AFBO", "CCC"] 
       } 

def groups = newList.groupBy {it -> it.name} 

def lowestIds = groups.collect({it.value.sort{it.id}[0]}) 

println lowestIds 

更簡單:

newList.sort{it.id}.unique{it.name} 

println newList 
0

這裏是一個解決腳本:

  • 未排序的項目,請注意,爲了在下面myList改變。
  • OP沒有提及重複的標準。這考慮基於name and code
def myList = [[id:'8100-07', name:'CCC', code:6089], 
       [id:'8100-04', name:'AAA', code:2281], 
       [id:'8100-05', name:'BBB', code:2102], 
       [id:'8100-06', name:'CCC', code:6089], 
       [id:'8100-08', name:'CCC', code:6089]] 


def newList = myList.findAll { 
       it.get("name").equals("AAA") || 
       it.get("name").equals("BBB") || 
       it.get("name").equals("AFBO") || 
       it.get("name").equals("CCC") 
} 
//Sort by order id, name and code fields 
def criteria = { a,b -> a.id <=> b.id ?: a.name <=> b.name ?: a.code?: b.code } 
//Group by name and code ; apply sort; get the first item; apply sort on final result 
def result = newList.groupBy({it.name}, {it.code}).inject([]){li, k, v-> v.collect {key, value -> li << value.sort(criteria)[0]}; li}.sort(criteria) 
println result 

重複的,可以快速嘗試網上Demo