Reputation: 6451
I have an Object MyObject
which has a List of ThatObjects
where each ThatObject
is a list of ThoseObjects
.
MyObject {
List<ThatObject> thatObjects;
}
ThatObject {
List<ThoseObject> thoseObjects
}
If I have a handle to MyObject, is it possible to get a handle to all thoseObjects in one list joined to together? Without have to iterate and make the joined list myself?
Thanks
Upvotes: 0
Views: 1296
Reputation: 171054
Given:
class MyObject {
List thatObjects
}
class ThatObject {
List thoseObjects
}
We can make a test object of:
def o = new MyObject( thatObjects:[ new ThatObject( thoseObjects:[ 1, 2 ] ),
new ThatObject( thoseObjects:[ 3, 4 ] ) ] )
Then a simple walk through the properties gives us:
assert o.thatObjects.thoseObjects == [ [1, 2], [3, 4] ]
And call flatten
to get a single list:
assert o.thatObjects.thoseObjects.flatten() == [ 1, 2, 3, 4 ]
Or, you could use collectMany
assert o.thatObjects.collectMany { it.thoseObjects } == [ 1, 2, 3, 4 ]
Upvotes: 2