繁体   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