0

I have a multikey map. Though it's a long map, but for understanding I am just showing 3 elements with 4 fields each. key1 and key2 are keys for this multikey map.

Map:

{MultiKey[M3, M33]=Row(field1=2021-02-28, field2=3,key1=M3,key2=M33), MultiKey[M1, M11]=Row(field1=2021-02-28, field2=3,key1=M1,key2=M11), MultiKey[M2, M22]=Row(field1=2021-02-28, field2=2, key1=M2,key2=M22))}

My POJO class looks like:

@Data
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Row implements Serializable {
    public String field1;
    public String field2;
    public String key1;
    public String key2;

    public String toString() {
         return new ToStringBuilder(this).
           append("field1", field1).
           append("field2", field2).
           append("key1", key1).
           append("key2", key2).
           toString();
    }
}

I want to convert values of this map to objects. I am trying to do it in below way.

ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
Row[] pojo1 = mapper.convertValue(map.values(), Row[].class);

I went through https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringBuilder.html as suggested in the comments.

System.out.println("MATCH KEY1: +ToStringBuilder.reflectionToString(pojo1[0].key1));

MATCH KEY1: java.lang.String@372ea2bc[value={M,3},hash=2438]

But I expect it to print just M3.

Abhi
  • 309
  • 1
  • 10
  • Looks similar to https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo – pyb Feb 11 '22 at 16:45
  • Does this answer your question? [Convert a Map to a POJO](https://stackoverflow.com/questions/16428817/convert-a-mapstring-string-to-a-pojo) – pyb Feb 11 '22 at 16:45
  • No, I had a look at this question before asking another one. My requirement is to create pojo for values of a Multikeymap. Unfortunately that solution didn't help me. – Abhi Feb 11 '22 at 16:52
  • 1
    ok. The reason `println` prints `[Lcom.abc.project.models.Row;@32232e55` is because you have not implemented the [`toString()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString\(\)) method. You can get it auto generated : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringBuilder.html or https://www.projectlombok.org/features/ToString – pyb Feb 11 '22 at 17:08
  • @pyb: I used - System.out.println(ToStringBuilder.reflectionToString(map3)); but it gave me result like - org.apache.commons.collections.map.MultiKeyMap@c65a5ef[map=org.apache.commons.collections.map.HashedMap@770d4269] – Abhi Feb 11 '22 at 18:53
  • Not on your map, on the row like the example you provided. You wanted to verify the conversion right? You can also set a breakpoint and start the debugger (it's fun) or write a simple unit test such as `assertEquals("hello", row.getStringOne())` – pyb Feb 11 '22 at 18:55
  • 1
    @pyb: Sorry, It was my bad. I wanted to print pojo1 instead of map. I put the breakpoint and printed pojo1 as well. My all elements are visible. But I am struggling to access those. There is no get method coming for me to access those. In my question, I gave my updated Pojo class as well . Do you see where am I missing something? – Abhi Feb 11 '22 at 21:00
  • your POJO fields are public so you can call pojo1.field1 directly. Otherwise you need to add getters. Like toString, you can either write them yourselves or use a tool like Lombok and add the annotation `@Data` or `@Getters` on top of your class (right before `public class Pojo`). – pyb Feb 11 '22 at 21:12
  • Could you please update the result of println and what you expect versud what you get? – pyb Feb 11 '22 at 21:12
  • @pyb: I update my pojo class, and print statement output. Can you please go through it and suggest if any possible solution is there – Abhi Feb 12 '22 at 07:25
  • @pyb : Expected: M3, Actual: java.lang.String@372ea2bc[value={M,3},hash=2438] – Abhi Feb 14 '22 at 07:22

0 Answers0