java 将 POJO 转换为 <K,V> 映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6075385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a POJO to a <K,V> map
提问by Victor
Possible Duplicate:
How to convert a Java object (bean) to key-value pairs (and vice versa)?
What is the best way to convert a List<POJO> to a List<Map<K,V>>.
Is there a custom method/ API?
转换的最佳方法List<POJO> to a List<Map<K,V>>.
是什么?是否有自定义方法/API?
K = field name of the POJO and V is the corresponding value
K = POJO 的字段名,V 是对应的值
public class POJO implements Serializable{
String name;
String age;
//getters and setters
}
回答by Anthony Accioly
Sounds like a job for the good and old Introspector.
听起来像一个好老的自省的工作。
Working example:
工作示例:
// Don't be lazy like this, do something about the exceptions
public static void main(String[] args) throws Exception {
List<POJO> pojos = new ArrayList<POJO>();
POJO p1 = new POJO();
p1.setAge("20");
p1.setName("Name");
pojos.add(p1);
POJO p2 = new POJO();
// ...
System.out.println(convertCollection(pojos));
}
public static List<Map<String, ?>> convertCollection(Collection collection)
throws Exception {
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
for (Object element : collection) {
list.add(getValues(element));
}
return list;
}
public static Map<String, ?> getValues(Object o)
throws Exception {
Map<String, Object> values = new HashMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(o.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
// This will access public properties through getters
Method getter = pd.getReadMethod();
if (getter != null)
values.put(pd.getName(), getter.invoke(o));
}
return values;
}
回答by Kaj
You can use reflection to do that. See Class.getDeclaredFields. That will give you the fields of a class, you can then get the values from them and populate your map.
您可以使用反射来做到这一点。请参阅Class.getDeclaredFields。这将为您提供一个类的字段,然后您可以从中获取值并填充您的地图。
Note that you might need to invoke setAccessibleon the fields if the are private before you can get the value.
请注意,如果字段是私有的,您可能需要在字段上调用setAccessible,然后才能获取值。
Edit: My answer only applies to the case where you don't know the fields / implementation of the POJO when you build the map.
编辑:我的回答仅适用于您在构建地图时不知道 POJO 的字段/实现的情况。
回答by monkHyman
If you mean a map of K,V then this will work
如果您的意思是 K,V 的地图,那么这将起作用
List<Pojo> pojos = ...;
Map<String, String> map = new HashMap<String,String>();
for (Pojo pojo : pojos) {
map.put(pojo.getName(), pojo.getAge());
}
回答by Adam Batkin
Assuming you want a Map
sans list, the easiest way is probably with a simple for loop:
假设您想要一个Map
sans 列表,最简单的方法可能是使用简单的 for 循环:
Map<K,V> theMap = new HashMap<K,V>();
for (POJO obj : theList) {
// Obviously the below can be simplified to one line
// but it makes sense to make everything explicit for
// ease of reading
K key = ... // obj.getName() maybe?
V value = ... // obj itself maybe?
theMap.put(key, value);
}
回答by Andreas Dolk
You need to know the POJO name. Assuming you have something like pojo.getName()
, then it goes like this:
您需要知道 POJO 名称。假设你有类似的东西pojo.getName()
,那么它是这样的:
Map<String, Pojo> pojoMap = new HashMap<String, Pojo>();
for (Pojo pojo:pojoList) {
pojoMap.put(pojo.getName(), pojo);
}
Notethat I changed your requirement, I've convertedone list of pojos to one map.
请注意,我更改了您的要求,我已将一份 pojo 列表转换为一张地图。