1

How to convert

List<Object[]> to List<POJOObject>

Here is example

//So, lets us say I have Object[], I want to have a class as follows:

class POJOObject {
    //maps to Object[0]
    private Integer x;
    //maps to Object[1]
    private Long y;
    //maps to Object[2]
    private String y;
}

Is there any easy way to do that? What library can be useful here?

Thanks.

2
  • 2
    If using Java 8 then just use a Stream. Otherwise write a loop. What exactly are you having trouble with? Commented Oct 24, 2014 at 21:10
  • You might use Dozer for it: dozer.sourceforge.net Commented Oct 24, 2014 at 21:16

1 Answer 1

2

Maybe http://dozer.sourceforge.net can help you. It is a mapping library configurable by xml.

I tried it shortly with this:

public class Main {
  public static void main(String[] args) {
    Object[] obj = new Object[3];
    obj[0] = new Integer(10);
    obj[1] = new Long(2346246234634L);
    obj[2] = "Hello";

    Collections.singletonList("mapping.xml");
    DozerBeanMapper mapper = new DozerBeanMapper(Collections.singletonList("mapping.xml"));
    PojoObject pojo = mapper.map(obj, PojoObject.class);
    System.out.println(pojo);
  }

  public static class PojoObject {
    private Integer integer;
    private Long longg;
    private String string;

    public PojoObject() {}

    public Integer getInteger() {
      return integer;
    }

    public void setInteger(Integer integer) {
      this.integer = integer;
    }

    public Long getLongg() {
      return longg;
    }

    public void setLongg(Long longg) {
      this.longg = longg;
    }

    public String getString() {
      return string;
    }

    public void setString(String string) {
      this.string = string;
    }

    @Override
    public String toString() {
      return String.format("Pojo content: %d, %d, %s", integer, longg, string);
    }
  }
}

My mappings.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">
    <mapping>
        <class-a>java.lang.Object[]</class-a>
    <class-b>ch.romix.dozertest.Main.PojoObject</class-b>
        <field>
        <a>this[0]</a>
        <b>Integer</b>
    </field>
    <field>
        <a>this[1]</a>
        <b>Longg</b>
    </field>
    <field>
        <a>this[2]</a>
        <b>String</b>
    </field>
    </mapping>
</mappings>

Unfortunately it only mapped 10 to all three PojoObject properties. Maybe you can see the error and use the snippet for your code. Maybe it is a bug in Dozer... I couldn't find any example using this[0].

Sign up to request clarification or add additional context in comments.

3 Comments

how? which method exactly?
@riship89 read this short tutorial: dozer.sourceforge.net/documentation/usage.html.
I am getting same value for all fields, any idea how to resolve it. Also fields are having parameter_address as value. "x": "[Ljava.lang.Object;@2d716a1c", "y": "[Ljava.lang.Object;@2d716a1c",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.