0

例如,我有一个对象列表,列表 A,它用 7 个元素初始化。每个元素都由一个称为“elementOrder”的整数字段排序。

如何获取相同对象的新列表 List B,并根据“elementOrder”将它们合并到 List A 中?

请注意,列表 B 包含列表 A 的重复项,我只想将列表 B 的唯一元素合并到列表 A 中。

谢谢。小号

4

1 回答 1

0
//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)
于 2017-03-20T19:02:33.383 回答