本文介绍了如何将地图列表转换为对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 POJO 类,如下所示: - $ / $>
公共类Pojo {
字符串参数1
列表参数2
映射参数3
布尔参数4
整数参数5
}
我有一个 List ,其中包含 Map >具有相同结构的对象,如 Pojo : -
列表列表= [
[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对象列表
任何人都知道如何转换它是一个单线......
$ b
解决方案
pre> //这里是我们要转换为的类
public class Pojo {
String param1
列表参数2
映射参数3
布尔参数4
整数参数5
}
//这里是我们要转换的数据
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,f:f],param4:false,param5:2]
]
//这是转换
List< Pojo> pojos = list.collect {new Pojo(it)}
I have a POJO class like as below :-
public class Pojo { String param1 List param2 Map param3 Boolean param4 Integer param5 }
I have a List which contains Map objects with the same structure as like 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] ]
Now I want to convert this list of maps to list of pojo objects
Anyone have idea how to convert it??
解决方案
It's a one-liner....
// 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) }
这篇关于如何将地图列表转换为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!