簡體   English   中英

如何在Gosu中將新元素合並到列表中

[英]How to merge new elements into a List in Gosu

例如,我有一個對象列表,列表A,並使用7個元素對其進行了初始化。 每個元素由稱為“ elementOrder”的整數字段排序。

如何獲取相同對象的新列表List B,並基於“ elementOrder”將它們合並到List A中?

請注意,列表B包含列表A的重復項,我只想將列表B的唯一元素合並到列表A中。

謝謝。 小號

//copy ListA element on a new list 
var newList = new ArrayList<ElementType>(ListA)
//add B elements to new list 
newList.addAll(ListB)
//order the new list with elementOrder column
newList = newList.orderBy( \ element -> element.elementOrder) 

問:列表B包含列表A的重復項,我只想將列表B的唯一元素合並到列表A中

R:您必須使用Blocks(lambda表達式)來過濾重復元素

//copy ListA element on a new list 
var newList = new ArrayList<ElementType>(ListA)
//filter B elements not in ListA
var FiltredListB = ListB.where( \ element -> not ListA.contains(element))
//add FiltredListB elements to new list 
newList.addAll(FiltredListB)
//order the new list with elementOrder column
newList = newList.orderBy( \ element -> element.elementOrder)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM