Subscribe to The Way I See It        RSS Feed
-----

Collecting attributes from a Collection of Objects

Icon Leave Comment
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:

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 ]

There are no Trackbacks for this entry

November 2017

S M T W T F S
   1234
567891011
12131415161718
192021 22 232425
2627282930  

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)