Java的Map与Object互转 - Bill zhang - 51CTO技术博客
Java的Map与Object互转 - Bill zhang - 51CTO技术博客

http://zlfwmm.blog.51cto.com 【复制】 【订阅】
- 主页
| - log4j
| - linux
| - Java中间件
| - it生活
| - 设计模式
| - java基础学��
| - 框架
| - web
| - hbase
| - java
| - 监控运维
| - storm
| - zookeeper
| - 缓存
| - 网络技术
| - 数据库
| - apache
| - hadoop
| - 淘宝技术
| - 知识链
zlfwmm 的BLOG
|
|—————————————————————————————————————-|
博客统计信息
用户名:zlfwmm
文章数:323
评论数:1
访问量:39502
无忧币:4670
博客积分:2696
博客等级:7
注册日期:2012-09-04
热门专题
Oracle零基础成长之路阅读量:1297

原来你也在这里(征文)阅读量:3317

从菜鸟到老鸟-教你玩转Mac操作系统阅读量:444774

QT学习之路:从入门到精通阅读量:1130522
热门文章
- ZooKeeper客户端连接数过多
- Java的Map与Object互转
- ActiveMQ死信队列使用
- Hadoop维护实战-设置hdfs..
- java并发容器(Map、List..
- linux 创建新用户并增加..
- 6种Java序列化框架
- ActiveMQ问题分析和解决
搜索BLOG文章
最近访客

冬瓜鸡汤
28609..
mb598..- %!(EXTRA markdown.ResourceType=, string=, string=)
smartdj - %!(EXTRA markdown.ResourceType=, string=, string=)
盘木 - %!(EXTRA markdown.ResourceType=, string=, string=)
itxia.. - %!(EXTRA markdown.ResourceType=, string=, string=)
暄暄.. - %!(EXTRA markdown.ResourceType=, string=, string=)
qq598.. - %!(EXTRA markdown.ResourceType=, string=, string=)
li秋.. - %!(EXTRA markdown.ResourceType=, string=, string=)
wx57d.. - %!(EXTRA markdown.ResourceType=, string=, string=)
xiaof.. - %!(EXTRA markdown.ResourceType=, string=, string=)
qq580..
最新评论
51CTO推荐博文
- RabbitMQ入门与使用篇
- Django 获取前端发送的头文件
- SQL Server事务日志分析
- 《Java从入门到放弃》入门篇:Str..
- 是什么优化让 .NET Core 性能飙升?
- 大话WEB前端性能优化基本套路
- Rust所有权语义模型
- 熊猫直播Rancho发布系统构建之路
- BeX5开发中MySQL视图使用的一个小..
- MySQL令人头疼的Aborted告警案例分析
- 日订单超1000万,美团外卖是如何..
友情链接
%!(EXTRA markdown.ResourceType=, string=, string=)
Java的Map与Object互转
2016-03-29 14:34:06
标签:Java Map Object
Java的Map与Object互转有三种方法:
1.使用 org.apache.commons.beanutils 进行转换;
使用简单方便,但需要引入第三方包。
2.使用 Introspector 进行转换;
getPropertyDescriptors()根据方法来得到属性信息,所有符合javabean的get、set方法都会被获取到,需要自己过滤不是属性的方法;
3.使用 reflect 进行转换;
getDeclaredFields()会获取所有属性,需要过滤不需要的类型如 static final等,不会获取父类的属性,需要自己编码实现
通过简单性能测试发现:
org.apache.commons.beanutils性能最差,使用 reflect 性能最好。
下面是代码示例:
|2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1|/**
``* 使用org.apache.commons.beanutils进行转换
``*/
class ApacheBeanUtils {
``/*
``* Populate the JavaBeans properties of the specified bean, based on
``* the specified name/value pairs.
``*/
``public Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
``if (map == null)
``return null;
``Object obj = beanClass.newInstance();
``org.apache.commons.beanutils.BeanUtils.populate(obj, map);
``return obj;
``}
``/*
``* An implementation of Map for JavaBeans which uses introspection to
``* get and put properties in the bean.
``*/
``public Map<?, ?> objectToMap(Object obj) ``throws Exception {
``if``(obj == ``null``)
``return null``;
``return new org.apache.commons.beanutils.BeanMap(obj);
``}
}
|
|——————————————————-|————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————|
|2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1|/**
``* 使用Introspector进行转换
``* getPropertyDescriptors()根据方法来得到属性信息,所有符合javabean的get、set方法都会被获取到
``* 需要自己过滤不是属性的方法
``* 耗时与reflect相比:100:1
``*/
public class IntrospectorBeanUtils{
``public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) ``throws Exception {
``if (map == ``null || map.size()<=``0``)
``return null``;
``Object obj = beanClass.newInstance();
``BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.``class``);
``PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
``for (PropertyDescriptor property : propertyDescriptors) {
``Method setter = property.getWriteMethod();
``if (setter != ``null``) {
``setter.invoke(obj, map.get(property.getName()));
``}
``}
``return obj;
``}
``public static Map<String, Object> objectToMap(Object obj) ``throws Exception {
``if``(obj == ``null``)
``return null``;
``Map<String, Object> map = ``new HashMap<String, Object>();
``BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.``class``);
``PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
``for (PropertyDescriptor property : propertyDescriptors) {
``String key = property.getName();
``if (key.compareToIgnoreCase(``"class"``) == ``0``) {
``continue``;
``}
``Method getter = property.getReadMethod();
``Object value = getter!=``null ? getter.invoke(obj) : ``null``;
``map.put(key, value);
``}
``return map;
``}
}
|
|———————————————————————————–|————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————-|
|2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
1|/**
``* 使用reflect进行转换
``* getDeclaredFields()会获取所有属性,需要过滤不需要的类型如 static final
``* 不会获取父类的属性,需要自己编码实现
``* 耗时与Introspector相比:1:100
``*/
public class ReflectBeanUtils{
``public static Object mapToObject(Map<String, String> map, Class<?> beanClass) ``throws Exception {
``if (map == ``null || map.size()<=``0``)
``return null``;
``Object obj = beanClass.newInstance();
``//获取关联的所有类,本类以及所有父类
``boolean ret = ``true``;
``Class oo = obj.getClass();
``List<Class> clazzs = ``new ArrayList<Class>();
``while``(ret){
``clazzs.add(oo);
``oo = oo.getSuperclass();
``if``(oo == ``null || oo == Object.``class``)``break``;
``}
``for``(``int i=``0``;i<clazzs.size();i++){
``Field[] fields = clazzs.get(i).getDeclaredFields();
``for (Field field : fields) {
``int mod = field.getModifiers();
``if``(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
``continue``;
``}
``//由字符串转换回对象对应的类型
``if (field != ``null``) {
``field.setAccessible(``true``);
``field.set(obj, map.get(field.getName()));
``}
``}
``}
``return obj;
``}
``public static Map<String, Object> objectToMap(Object obj) ``throws Exception {
``if``(obj == ``null``){
``return null``;
``}
``//获取关联的所有类,本类以及所有父类
``boolean ret = ``true``;
``Class oo = obj.getClass();
``List<Class> clazzs = ``new ArrayList<Class>();
``while``(ret){
``clazzs.add(oo);
``oo = oo.getSuperclass();
``if``(oo == ``null || oo == Object.``class``)``break``;
``}
``Map<String, Object> map = ``new HashMap<String, Object>();
``for``(``int i=``0``;i<clazzs.size();i++){
``Field[] declaredFields = clazzs.get(i).getDeclaredFields();
``for (Field field : declaredFields) {
``int mod = field.getModifiers();
``//过滤 static 和 final 类型
``if``(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
``continue``;
``}
``field.setAccessible(``true``);
``map.put(field.getName(), field.get(obj));
``}
``}
``return map;
``}
``public static void main(String[] args) ``throws Exception {
``System.out.println(``2222``);
``PersonBean pb = ``new PersonBean(``"name"``, ``15``, 12345678l, ``true``, ``"10086"``, ``"北京市"``);
``}
}
|
|———————————————————————————————————————————————–|—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————–|
|%!(EXTRA markdown.ResourceType=, string=, string=)
|.
|%!(EXTRA markdown.ResourceType=, string=, string=)
|
|————————————————–|-|—————————————————————————————-|
|%!(EXTRA markdown.ResourceType=, string=, string=)
|0人|了这篇文章|
|—————————————————|—|———-|
类别:java┆阅读(2596)┆评论(0) ┆返回博主首页┆返回博客首页
上一篇 Java反射与内省下一篇 Java序列化
相关文章
- Map的遍历方法
- Java遍历Map的两种实现方法
- 定制化高效使用Map的一些经验技巧
- java中对map根据value进行排序
- java中遍历map的优良方法之EntrySet
- 整理的部分Java和C#不同点
- 用EL表达式获取List<Map<String,Objec..
- java的类祖宗:object类
- JAVA对象的序列化和反序列化
文章评论
发表评论
%!(EXTRA markdown.ResourceType=, string=, string=)
昵 称:登录 快速注册验证码: 请点击后输入验证码博客过2级,无需填写验证码
内 容:
×
[**意见
反馈**](http://51ctoblog.blog.51cto.com/26414/1933645)
Copyright By 51CTO.COM 版权所有









