Convert List of one type to Array of another type using Dozer

Posted by aheu on Stack Overflow See other posts from Stack Overflow or by aheu
Published on 2010-06-18T02:18:42Z Indexed on 2010/06/18 2:23 UTC
Read the original article Hit count: 363

Filed under:
|
|
|
|

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(listOfA, 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!

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays