文章目录
  1. 1. Java的Map与Object互转 - Bill zhang - 51CTO技术博客
    1. 1.1. zlfwmm 的BLOG
    2. 1.2. 博客统计信息
    3. 1.3. 热门专题
    4. 1.4. 热门文章
    5. 1.5. 搜索BLOG文章
    6. 1.6. 最近访客
    7. 1.7. 最新评论
    8. 1.8. 51CTO推荐博文
    9. 1.9. 友情链接
    10. 1.10. 相关文章
    11. 1.11. 文章评论
    12. 1.12. 发表评论

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

top_bg_xian.gif

favorite_add_small.gif

首页ssk.png

我的博客icon01.png

推荐博文 top_shequ.gif

最新原创 avatar-php.jpg

专家博客 wKioL1kxBvqDXivDAAA4F7mg1So675.jpg

推荐博客 head_blog_sibebar_logo.gif

搜索
nav_ico1.gif

网站导航avatar-php-1.jpg

登录注册avatar-php.png

# Bill zhang

http://zlfwmm.blog.51cto.com 【复制】 【订阅】

原创:236翻译:0转载:87

zlfwmm 的BLOG

|wKioL1k_SpejGPpUAAA3gnotfPU742.png

|
|—————————————————————————————————————-|

发私信 加友情链接

博客统计信息

用户名:zlfwmm

文章数:323

评论数:1

访问量:39502

无忧币:4670

博客积分:2696

博客等级:7

注册日期:2012-09-04

热门专题

更多>>
vip_grey2.png

Oracle零基础成长之路阅读量:1297
1304672337_index.jpg

原来你也在这里(征文)阅读量:3317
lg-php.gif

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

QT学习之路:从入门到精通阅读量:1130522

热门文章

搜索BLOG文章

最近访客

最新评论

51CTO推荐博文

更多>>

友情链接

博主的更多文章>>

%!(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序列化

相关文章

文章评论

 

 

发表评论

%!(EXTRA markdown.ResourceType=, string=, string=)

昵  称:登录  快速注册验证码: 请点击后输入验证码博客过2级,无需填写验证码

内  容:

×

[**意见

反馈**](http://51ctoblog.blog.51cto.com/26414/1933645)

Copyright By 51CTO.COM 版权所有

%!(EXTRA markdown.ResourceType=, string=, string=)

文章目录
  1. 1. Java的Map与Object互转 - Bill zhang - 51CTO技术博客
    1. 1.1. zlfwmm 的BLOG
    2. 1.2. 博客统计信息
    3. 1.3. 热门专题
    4. 1.4. 热门文章
    5. 1.5. 搜索BLOG文章
    6. 1.6. 最近访客
    7. 1.7. 最新评论
    8. 1.8. 51CTO推荐博文
    9. 1.9. 友情链接
    10. 1.10. 相关文章
    11. 1.11. 文章评论
    12. 1.12. 发表评论