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

发布于 2024-09-06 06:36:01 字数 1579 浏览 7 评论 0原文

我想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-09-13 06:36:01

在开始迭代之前,您知道 listOfA 中有多少项。为什么不实例化 new B[listOfA.size()],然后迭代 A,将新的 B 实例直接放入数组中。您将节省对 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);
}

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.

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);
}
玉环 2024-09-13 06:36:01

好吧,所以我是个白痴。我太习惯了 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()]);

问题解决了!

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.

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!

陌上青苔 2024-09-13 06:36:01

如果我可以编写下面的代码并且它可以工作,那就更有意义了

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文