java反射机制完成List<map>与List<javabean>,List<对象>之间的

我们可以利用Java的反射机制list与list的就是list之间的互转,可以参考下面代码:

编写工具类

  1. public class ListUtil{
  2. /**
  3. * 将JavaBean转换成Map
  4. *
  5. * @param obj
  6. * @return
  7. * @throws SecurityException
  8. * @throws NoSuchMethodException
  9. * @throws InvocationTargetException
  10. * @throws IllegalArgumentException
  11. * @throws IllegalAccessException
  12. */
  13. public static Map beanToMap(Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException,
  14. IllegalArgumentException, InvocationTargetException {
  15. // 创建map集合
  16. Map map = new HashMap();
  17. // 获取JavaBean中所有属性
  18. Field[] fields = obj.getClass().getDeclaredFields();
  19. for (Field fie : fields) {
  20. // 将属性第一个字母转换成大写
  21. String frist = fie.getName().substring(0, 1).toUpperCase();
  22. // 获取属性的类型
  23. Class type = fie.getType();
  24. // 封装属性的get
  25. String getter = "";
  26. if ("boolean".equals(type.getName())) {
  27. getter = "is" + frist + fie.getName().substring(1);
  28. } else {
  29. getter = "get" + frist + fie.getName().substring(1);
  30. }
  31. // 获取JavaBean的方法
  32. Method method = obj.getClass().getMethod(getter, new Class[] {});
  33. // 调用方法,并接收返回值
  34. Object objec = method.invoke(obj, new Object[] {});
  35. // 判断返回值不为空
  36. if (objec != null) {
  37. map.put(fie.getName(), objec);
  38. } else {
  39. map.put(fie.getName(), "");
  40. }
  41. }
  42. return map;
  43. }
  44. /**
  45. * 将Map转换为JavaBean
  46. *
  47. * @param map
  48. * @param obj
  49. * @return
  50. * @throws SecurityException
  51. * @throws NoSuchMethodException
  52. * @throws InvocationTargetException
  53. * @throws IllegalArgumentException
  54. * @throws IllegalAccessException
  55. */
  56. public static Object mapToBean(Map<String, Object> map, Object obj) throws NoSuchMethodException, SecurityException,
  57. IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  58. // 获取JavaBean中的所有属性
  59. Field[] field = obj.getClass().getDeclaredFields();
  60. for (Field fi : field) {
  61. // 判断key值是否存在
  62. if (map.containsKey(fi.getName())) {
  63. // 获取key的value值
  64. String value = map.get(fi.getName()).toString();
  65. // 将属性的第一个字母转换为大写
  66. String frist = fi.getName().substring(0, 1).toUpperCase();
  67. // 属性封装set方法
  68. String setter = "set" + frist + fi.getName().substring(1);
  69. // 获取当前属性类型
  70. Class type = fi.getType();
  71. // 获取JavaBean的方法,并设置类型
  72. Method method = obj.getClass().getMethod(setter, type);
  73. // 判断属性为double类型
  74. if ("java.lang.String".equals(type.getName())) {
  75. // 调用当前Javabean中set方法,并传入指定类型参数
  76. method.invoke(obj, value);
  77. } else if ("int".equals(type.getName())) {
  78. method.invoke(obj, Integer.parseInt(value));
  79. }else if ("double".equals(type.getName())) {
  80. method.invoke(obj, Double.valueOf(value));
  81. } else if ("char".equals(type.getName())) {
  82. method.invoke(obj, value);
  83. }
  84. }
  85. }
  86. return obj;
  87. }
  88. /**
  89. * 将List>转换成List
  90. *
  91. * @param listm
  92. * @param obj
  93. * @return
  94. * @throws InvocationTargetException
  95. * @throws IllegalArgumentException
  96. * @throws IllegalAccessException
  97. * @throws SecurityException
  98. * @throws NoSuchMethodException
  99. */
  100. public static Object ListMapToListBean(List<Map<String, Object>> listm, Object obj)
  101. throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
  102. InvocationTargetException {
  103. List<Object> list = new ArrayList<Object>();
  104. // 循环遍历出map对象
  105. for (Map<String, Object> m : listm) {
  106. // 调用将map转换为JavaBean的方法
  107. Object objs = mapToBean(m, obj);
  108. // 添加进list集合
  109. list.add(objs);
  110. }
  111. return list;
  112. }
  113. /**
  114. * 将list转换为List
  115. *
  116. * @param list
  117. * @return
  118. * @throws NoSuchMethodException
  119. * @throws SecurityException
  120. * @throws IllegalAccessException
  121. * @throws IllegalArgumentException
  122. * @throws InvocationTargetException
  123. */
  124. public static List<Map<String, Object>> ListBeanToListMap(List<Object> list) throws NoSuchMethodException,
  125. SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  126. List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
  127. for (Object ob : list) {
  128. listmap.add(beanToMap(ob));
  129. }
  130. return listmap;
  131. }

}
用法:

  1. public class Test{
  2. private String name;
  3. //setting和gentting
  4. }
  5. //将List>转换成List
  6. List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
  7. Map<String, Object> map=new HashMap<String, Object>();
  8. map.add("name","shusheng");
  9. list.add(map);
  10. List<Test> test=ListUtil.ListMapToListBean(list,new Test());

注释已经很多了,不明白的可以留言提问。