简体   繁体   English

使用推土机将一种类型的列表转换为另一种类型的数组

[英]Convert List of one type to Array of another type using Dozer

I'm wondering how to convert a List of one type to an array of another type in Java using Dozer. 我想知道如何使用Dozer在Java中将一种类型的列表转换为另一种类型的数组。 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类。

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 ab[]. 如果我创建两个包装器类,一个包含一个List,另一个包含ab [],则mapper.map函数可以正常工作。 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)? 在不使用包装器类(C和D)的情况下如何做我想做的事情? There has got to be an easier way... Thanks! 必须有一种更简单的方法...谢谢!

You know how many items are in listOfA before you start iterating. 在开始迭代之前,您知道listOfA中有多少个项目。 Why not instantiate new B[listOfA.size()] and then iterate over A, putting your new B instances directly in the array. 为什么不实例化新的B [listOfA.size()],然后遍历A,将新的B实例直接放在数组中。 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. 您将为listOfB中的所有项目节省额外的迭代时间,并且该代码实际上将更易于阅读和引导。

Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();

List<A> listOfA = getListofAObjects();
B[] arrayOfB = new B[listOfA.size()];

int i = 0;
for (A a : listOfA) {
    arrayOfB[i++] = mapper.map(a, B.class);
}

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. 我已经习惯了Dozer为我完成所有工作……我要做的就是遍历A的列表并创建B的列表,然后将该列表转换为B的数组。

Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
List<A> listOfA = getListofAObjects();
Iterator<A> iter = listOfA.iterator();
List<B> listOfB = new ArrayList<B>();
while(iter.hasNext()){
   listOfB.add(mapper.map(iter.next(), B.class));
}
B[] bs = listOfB.toArray(new B[listOfB.size()]);

Problem Solved! 问题解决了!

It will make more sense if I could write below code and it works 如果我可以编写下面的代码,它将更有意义

List<A> listOfA = getListofAObjects();
Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
B[] bs = mapper.map(listOfA, B[].class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM