java使用泛型实现Bean类和Map的相互转换

java使用泛型实现Bean类和Map的相互转换,使用泛型可以这带来了很多好处:

      首要就是类型安全, Java 程序的类型安全。通过知道使用泛型,这些假设就只存在于程序员的头脑中(或者如果幸运的话,还存在于代码注释中)。

      泛型允许编译器实施这些附加的类型约束。类型错误现在就可以在编译时被捕获了,而不是在运行时当作 ClassCastException 展示出来。

    将类型检查从运行时挪到编译时有助于您更容易找到错误,并可提高程序的可靠性。

  消除强制类型转换。 泛型的一个附带好处是,消除源代码中的许多强制类型转换。这使得代码更加可读,并且减少了出错机会。

主方法實現:

 1 public class BeanUtil {
 2 
 3     public static void main(String[] args) throws Exception {
 4 
 5         Map<String, Object> mp = new HashMap<String, Object>();
 6         mp.put("name", "Jack");
 7         mp.put("age", 40);
 8         mp.put("mN", "male");
 9 
10         PersonBean person = map2Bean(mp, PersonBean.class);
11         System.out.println("transMap2Bean Map Info:");
12         for (Map.Entry<String, Object> entry : mp.entrySet()) {
13             System.out.println(entry.getKey() + ": " + entry.getValue());
14         }
15         System.out.println("Bean Info:");
16         System.out.println("name: " + person.getName());
17         System.out.println("age: " + person.getAge());
18         System.out.println("mN: " + person.getmN());
19 
20         bean2Map(person, mp);
21         System.out.println("transBean2Map Map Info:");
22         for (Map.Entry<String, Object> entry : mp.entrySet()) {
23             System.out.println(entry.getKey() + ": " + entry.getValue());
24         }
25     }

mapToBean方法:

 1     public static <T, K, V> T map2Bean(Map<K, V> mp, Class<T> beanCls)
 2             throws Exception, IllegalArgumentException, InvocationTargetException {
 3         T t = null;
 4         try {
 5 
 6             BeanInfo beanInfo = Introspector.getBeanInfo(beanCls);
 7             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
 8 
 9             t = beanCls.newInstance();
10 
11             for (PropertyDescriptor property : propertyDescriptors) {
12                 String key = property.getName();
13 
14                 if (mp.containsKey(key)) {
15                     Object value = mp.get(key);
16                     Method setter = property.getWriteMethod();// Java中提供了用来访问某个属性的
17                                                                 // getter/setter方法
18                     setter.invoke(t, value);
19                 }
20             }
21 
22         } catch (IntrospectionException e) {
23 
24             e.printStackTrace();
25         }
26         return t;
27     }

beanToMap方法:

 1 public static <T, K, V> Map<String, Object> bean2Map(T bean, Map<String, Object> mp)
 2             throws Exception, IllegalAccessException {
 3 
 4         if (bean == null) {
 5             return null;
 6         }
 7 
 8         try {
 9             BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
10             PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
11 
12             for (PropertyDescriptor property : propertyDescriptors) {
13                 String key = property.getName();
14 
15                 if (!key.equals("class")) {
16 
17                     Method getter = property.getReadMethod();// Java中提供了用来访问某个属性的
18                                                                 // getter/setter方法
19                     Object value;
20 
21                     value = getter.invoke(bean);
22                     mp.put(key, value);
23                 }
24 
25             }
26 
27         } catch (IntrospectionException e) {
28 
29             e.printStackTrace();
30         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33 
34         }
35         return mp;
36 
37     }
38 }

  總結:使用泛型可以避免類型轉換錯誤,可以在集合框架(Collection framework)中看到泛型的动机。例如,Map 类允许您向一个 Map 添加任意类的对象,

即使最常见的情况是在给定映射(map)中保存某个特定类型(比如 String)的对象。因为 Map.get() 被定义为返回 Object,所以一般必须将 Map.get() 的结果强制类型转换为期望的类型,如下面的代码所示:

Map m = new HashMap();
m.put("key", "blarg");
String s = (String) m.get("key");

要让程序通过编译,必须将 get() 的结果强制类型转换为 String,并且希望结果真的是一个 String。但是有可能某人已经在该映射中保存了不是 String 的东西,这样的话,上面的代码将会抛出 ClassCastException。

热血高校3结局里旋风雄能打得过林田惠吗?
原文地址:https://www.cnblogs.com/crows/p/4702975.html