将POJO转换为Map

huangapple go评论82阅读模式
英文:

Convert POJO to Map<String, String>?

问题

I am looking for a utility to convert an entire pojo to Map&lt;String,String&gt;. I can see examples all over, where pojo is converted to Map&lt;String,Object&gt;. But I am specifically looking for Map&lt;String,String&gt;.

英文:

I am looking for a utility to convert an entire pojo to Map&lt;String,String&gt;.
I can see examples all over, where pojo is converted to Map&lt;String,Object&gt;.

But i am specifically looking for Map&lt;String,String&gt;.

答案1

得分: 1

以下是翻译好的代码部分:

这里重要的是理解在POJO方面键和值将是什么您可以覆盖POJO的toString方法并将其用作值

public class Main {

    public static void main(String[] args) {

        Map<String, String> result = new HashMap<>();
        result.put("key1", new POJO("Done").toString());
        result.put("key2", new POJO("Clarke").toString());

    }
}


public class POJO {
    private String name;

    public POJO(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}
如果字段名用作键您可以使用反射请注意如果将POJO中的字段修饰符更改为private则需要稍微更改代码阅读有关反射的信息

public class Main {

    public static void main(String[] args) throws IllegalAccessException {

        Map<String, String> result = new HashMap<>();

        POJO pojo = new POJO("okGoogle", 12345);
        Class classPOJO = pojo.getClass();

        Field[] allFields = classPOJO.getFields();
        for (Field field : allFields) {
            String key = field.getName();
            String value = null;
            if (field.get(pojo) != null) {
                value = field.get(pojo).toString();
            }
            result.put(key, value);
        }

        System.out.println(result.get("name"));
        System.out.println(result.get("id"));

    }
}

POJO模型:

public class POJO {
    public String name;
    public int id;

    public POJO(String name, int id) {
        this.name = name;
        this.id = id;
    }
}
英文:

Here it is important to understand what will be the key and value in terms of POJO. You can override POJO's toString and use it as a value.

public class Main {

    public static void main(String[] args) {

        Map&lt;String, String&gt; result = new HashMap&lt;&gt;();
        result.put(&quot;key1&quot;, new POJO(&quot;Done&quot;).toString());
        result.put(&quot;key2&quot;, new POJO(&quot;Clarke&quot;).toString());

    }
}


public class POJO {
    private String name;

    public POJO(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

If the field name is used as the key, you can use reflection. Be careful, if you change the field modifier in POJO to private, you will need to change the code a little. Read about reflection.

public class Main {

public static void main(String[] args) throws IllegalAccessException {

    Map&lt;String, String&gt; result = new HashMap&lt;&gt;();

    POJO pojo = new POJO(&quot;okGoogle&quot;, 12345);
    Class classPOJO = pojo.getClass();

    Field [] allFields = classPOJO.getFields();
    for(Field field : allFields){
        String key = field.getName();
        String value = null;
        if( field.get(pojo) != null){
            value = field.get(pojo).toString();
        }
            result.put(key, value);
        }

        System.out.println(result.get(&quot;name&quot;));
        System.out.println(result.get(&quot;id&quot;));

    }
}

and POJO model

public class POJO {
    public String name;
    public int id;

    public POJO(String name, int id) {
        this.name = name;
        this.id = id;
    }

}

答案2

得分: 0

I am converting each pojo to map and then creating a map to store all maps representing the pojo class.

public class Main {

    public static void main(String[] args) {

        Person person = new Person();
        person.setName("Abhishek");
        person.setAge("30");

        Person person1 = new Person();
        person1.setName("Abhi");
        person1.setAge("28");

        Map<String, Map<String, String>> resultMap = new HashMap<>();
        Map<String, String> result = new HashMap<>();
        result.put("name", person.getName());
        result.put("age", person.getAge());
        resultMap.put("object1", result);

        result = new HashMap<>();
        result.put("name", person1.getName());
        result.put("age", person1.getAge());
        resultMap.put("object2", result);
    }
}

public class Person {
    private String name;

    private String age

    //setters and getters

}

Note: I have left the code part unchanged and provided the translation for the surrounding text.

英文:

i am converting each pojo to map and than creating a map to store all maps representing
pojo class.

public class Main {

    public static void main(String[] args) {

    Person person = new Person();
           person.setName(&quot;Abhishek&quot;);
           person.setAge(&quot;30&quot;);

    Person person1 = new Person();
           person1.setName(&quot;Abhi&quot;);
           person1.setAge(&quot;28&quot;);

    Map&lt;String,  Map&lt;String, String&gt;&gt; resultMap = new HashMap&lt;&gt;();
    Map&lt;String, String&gt; result = new HashMap&lt;&gt;();
    result.put(&quot;name&quot;, person.getName());
    result.put(&quot;age&quot;, person.getAge());
    resultMap.put(&quot;object1&quot;, result);

    result = new HashMap&lt;&gt;();
    result.put(&quot;name&quot;, person1.getName());
    result.put(&quot;age&quot;, person1.getAge());
        resultMap.put(&quot;object2&quot;, result);
    }
}

public class Person {
    private String name;

    private String age

   //setters and getters

}

huangapple
  • 本文由 发表于 2020年8月13日 22:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定