英文:
Convert POJO to Map<String, String>?
问题
I am looking for a utility to convert an entire pojo to Map<String,String>
. I can see examples all over, where pojo is converted to Map<String,Object>
. But I am specifically looking for Map<String,String>
.
英文:
I am looking for a utility to convert an entire pojo to Map<String,String>
.
I can see examples all over, where pojo is converted to Map<String,Object>
.
But i am specifically looking for Map<String,String>
.
答案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<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;
}
}
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<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"));
}
}
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("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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论