Groovy provides us with a slick way to iterate over objects in a collection to collet specific attributes. This is called the Spread operator ("*.")
For example:
Say we have a collection of people:
Now, I want to get a list of only the names:
The output would be:
Keep it Groovy,
Luckless
For example:
class Person{ String name; int age; }
Say we have a collection of people:
def people = [] //parameter maps are for another day people.add(new Person(name: 'Dave', age: '24')) people.add(new Person(name: 'Ann', age: '22')) people.add(new Person(name: 'Steve', age: '45'))
Now, I want to get a list of only the names:
println(people*.name)
The output would be:
['Dave', 'Ann', 'Steve']
Keep it Groovy,
Luckless
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← April 2021 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |
My Blog Links
Recent Entries
-
-
-
-
Collecting attributes from a Collection of Objects
on Aug 27 2013 01:47 PM
-
Recent Comments
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)