如何从Groovy中的对象字段中提取新列表

时间:2012-06-24 13:01:01

标签: groovy

在Groovy中,如何从以下内容中提取新列表:

   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
   }

以便新列表如下所示:

 ["Tom", "Abigail", "Joyce", "James", "Scott", "Ruth"]    

1 个答案:

答案 0 :(得分:24)

你可以这样做:

def names = people.name