本篇文章給大家分享的是有關java項目中Map與Object如何實現相互轉換,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
java Map轉Object與 Object轉Map
1、定義一個實體類:
package reflect;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}2、Map轉Object轉換如下:
package reflect;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class testMapToObject {
public static void main(String[] args){
//假設有一個Map存放了一個對象的姓名和年齡
Map<string,object> map = new HashMap<string,object>();
map.put("name", "Kobi");
map.put("age", 39);
User user = transferMapToUser(map,User.class);
System.out.println(user.getName());
System.out.println(user.getAge());
}
public static <t> T transferMapToUser(Map<string,object> map,Class<t> classT){
try {
return transfer(map,classT.newInstance());
} catch (InstantiationException ex) {
System.out.println("what the fuck?");
} catch (IllegalAccessException ex) {
System.out.println("what the fuck?");
}
return null;
}
public static <t> T transfer(Map<string,object> map,Object obj){
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property對應的setter方法
Method setter = property.getWriteMethod();
try {
setter.invoke(obj, value);
} catch (IllegalArgumentException ex) {
System.out.println("what the fuck?");
}
}
}
} catch (Exception ex) {
System.out.println("what the fuck?");
}
return (T) obj;
}
}運行結果:
run: Kobi 39 成功構建 (總時間: 0 秒)
3、Object轉Map:
package reflect;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.TreeMap;
public class testObjectToMap {
public static void main(String[] args){
User user = new User();
user.setName("Kobi");
user.setAge(39);
Map<string, object=""> map = transBean2Map(user);
System.out.println(map);
}
public static Map<string, object=""> transBean2Map(Object obj) {
if (obj == null) {
return null;
}
TreeMap<string, object=""> map = new TreeMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 過濾class屬性
if (key.equals("class")) {
continue;
}
// 得到property對應的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
}運行結果:
run:
{age=39, name=Kobi}
成功構建 (總時間: 0 秒)以上就是java項目中Map與Object如何實現相互轉換,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。