我有一个对象MyObject
,其列表为ThatObjects
,其中每个ThatObject
都是ThoseObjects
的列表。
MyObject {
List<ThatObject> thatObjects;
}
ThatObject {
List<ThoseObject> thoseObjects
}
如果我有MyObject的句柄,是否可以获得一个列表中所有那些对象的句柄?无需迭代并自己制作加入列表?
由于
答案 0 :(得分:2)
假设:
class MyObject {
List thatObjects
}
class ThatObject {
List thoseObjects
}
我们可以制作一个测试对象:
def o = new MyObject( thatObjects:[ new ThatObject( thoseObjects:[ 1, 2 ] ),
new ThatObject( thoseObjects:[ 3, 4 ] ) ] )
然后简单地浏览一下这些属性就可以了:
assert o.thatObjects.thoseObjects == [ [1, 2], [3, 4] ]
并致电flatten
以获取单个列表:
assert o.thatObjects.thoseObjects.flatten() == [ 1, 2, 3, 4 ]
或者,您可以使用collectMany
assert o.thatObjects.collectMany { it.thoseObjects } == [ 1, 2, 3, 4 ]