使用 Dozer 将一种类型的列表转换为另一种类型的数组
我想知道如何使用 Dozer 将 Java 中一种类型的列表转换为另一种类型的数组。这两种类型具有相同的属性名称/类型。 例如,考虑这两个类。
public class A{
private String test = null;
public String getTest(){
return this.test
}
public void setTest(String test){
this.test = test;
}
}
public class B{
private String test = null;
public String getTest(){
return this.test
}
public void setTest(String test){
this.test = test;
}
}
我试过这个但没有运气。
List<A> listOfA = getListofAObjects();
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
B[] bs = mapper.map(listOfA, B[].class);
我还尝试过使用 CollectionUtils 类。
CollectionUtils.convertListToArray(listOfA, B.class)
两者都不为我工作,有人能告诉我我做错了什么吗?如果我创建两个包装类,一个包含 List,另一个包含 ab[],则 mapper.map 函数可以正常工作。见下文:
public class C{
private List<A> items = null;
public List<A> getItems(){
return this.items;
}
public void setItems(List<A> items){
this.items = items;
}
}
public class D{
private B[] items = null;
public B[] getItems(){
return this.items;
}
public void setItems(B[] items){
this.items = items;
}
}
这工作起来很奇怪......
List<A> listOfA = getListofAObjects();
C c = new C();
c.setItems(listOfA);
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
D d = mapper.map(c, D.class);
B[] bs = d.getItems();
我如何在不使用包装类(C&amp; D)的情况下做我想做的事情?必须有一个更简单的方法...... 谢谢!
I'm wondering how to convert a List of one type to an array of another type in Java using Dozer. The two types have all the same property names/types.
For example, consider these two classes.
public class A{
private String test = null;
public String getTest(){
return this.test
}
public void setTest(String test){
this.test = test;
}
}
public class B{
private String test = null;
public String getTest(){
return this.test
}
public void setTest(String test){
this.test = test;
}
}
I've tried this with no luck.
List<A> listOfA = getListofAObjects();
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
B[] bs = mapper.map(listOfA, B[].class);
I've also tried using the CollectionUtils class.
CollectionUtils.convertListToArray(listOfA, B.class)
Neither are working for me, can anyone tell me what I am doing wrong? The mapper.map function works fine if I create two wrapper classes, one containing a List and the other a b[]. See below:
public class C{
private List<A> items = null;
public List<A> getItems(){
return this.items;
}
public void setItems(List<A> items){
this.items = items;
}
}
public class D{
private B[] items = null;
public B[] getItems(){
return this.items;
}
public void setItems(B[] items){
this.items = items;
}
}
This works oddly enough...
List<A> listOfA = getListofAObjects();
C c = new C();
c.setItems(listOfA);
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
D d = mapper.map(c, D.class);
B[] bs = d.getItems();
How do I do what I want to do without using the wrapper classes (C & D)? There has got to be an easier way...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在开始迭代之前,您知道 listOfA 中有多少项。为什么不实例化 new B[listOfA.size()],然后迭代 A,将新的 B 实例直接放入数组中。您将节省对 listOfB 中所有项目的额外迭代,并且代码实际上更易于阅读和启动。
You know how many items are in listOfA before you start iterating. Why not instantiate new B[listOfA.size()] and then iterate over A, putting your new B instances directly in the array. You'll save yourself an extra iteration over all of the items in listOfB and the code will actually be easier to read to boot.
好吧,所以我是个白痴。我太习惯了 Dozer 为我做所有的工作...我所需要做的就是迭代 A 列表并创建 B 列表,然后将该列表转换为 B 数组。
问题解决了!
Ok, so I'm an idiot. I was too used to Dozer doing all the work for me... All I needed to do was iterate over the List of A's and create a list of B's and then convert that list to an array of B's.
Problem Solved!
如果我可以编写下面的代码并且它可以工作,那就更有意义了
It will make more sense if I could write below code and it works