java Map转Object与Object转Map实现代码
程序员文章站
2024-03-07 22:42:39
java map转object与 object转map
1、定义一个实体类:
package reflect;
public class user...
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 秒)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: 深入理解Spring Boot的日志管理