Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a new list from an object field in Groovy

Tags:

groovy

In Groovy, how do I extract a new list from the following:

   def people = [ 
           new Person(name:"Tom", yearOfBirth:1985),
           new Person(name:"Abigail", yearOfBirth:1987),
           new Person(name:"Joyce", yearOfBirth:1984),
           new Person(name:"James", yearOfBirth:1987),
           new Person(name:"Scott", yearOfBirth:1985),
           new Person(name:"Ruth", yearOfBirth:1984)
       ]  

   class Person {
       String name
       int yearOfBirth
   }

so that the new list looks like this:

 ["Tom", "Abigail", "Joyce", "James", "Scott", "Ruth"]    
like image 898
Reimeus Avatar asked Jun 24 '12 13:06

Reimeus


1 Answers

You can do:

def names = people.name
like image 89
tim_yates Avatar answered Oct 15 '22 08:10

tim_yates