如何将地图列表转换为对象列表

时间:2016-07-01 10:01:39

标签: groovy

我有一个POJO类,如下所示: -

public class Pojo {
   String param1
   List param2
   Map param3
   Boolean param4
   Integer param5
}

我有一个List,其中包含与Map结构相同的Pojo个对象: -

List list = [
        [param1: "a", param2: ["a","b","c"], param3:[a:"a",b:"b",c:"c"], param4:true, param5:1],
        [param1: "b", param2: ["d","e","f"], param3:[d:"d",e:"e",f:"f"], param4:false, param5:2]
]

现在我想将此地图列表转换为 pojo对象列表

任何人都知道如何转换它?

1 个答案:

答案 0 :(得分:1)

这是一个单行......

// here's the class we want to convert to 
public class Pojo {
   String param1
   List param2
   Map param3
   Boolean param4
   Integer param5
}

// here the data we want to convert
List list = [
        [param1: "a", param2: ["a","b","c"], param3:[a:"a",b:"b",c:"c"], param4:true, param5:1],
        [param1: "b", param2: ["d","e","f"], param3:[d:"d",e:"e",f:"f"], param4:false, param5:2]
]

// this is the conversion
List<Pojo> pojos = list.collect { new Pojo(it) }