java使用泛型实现Bean类和Map的相互转换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java使用泛型实现Bean类和Map的相互转换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3285字,纯文字阅读大概需要5分钟。
内容图文
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"); 910 PersonBean person = map2Bean(mp, PersonBean.class); 11 System.out.println("transMap2Bean Map Info:"); 12for (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()); 1920 bean2Map(person, mp); 21 System.out.println("transBean2Map Map Info:"); 22for (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) 2throws Exception, IllegalArgumentException, InvocationTargetException { 3 T t = null; 4try { 5 6 BeanInfo beanInfo = Introspector.getBeanInfo(beanCls); 7 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 8 9 t = beanCls.newInstance(); 1011for (PropertyDescriptor property : propertyDescriptors) { 12 String key = property.getName(); 1314if (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 } 2122 } catch (IntrospectionException e) { 2324 e.printStackTrace(); 25 } 26return t; 27 }
beanToMap方法:
1 public static <T, K, V> Map<String, Object> bean2Map(T bean, Map<String, Object> mp) 2throws Exception, IllegalAccessException { 3 4if (bean == null) { 5returnnull; 6 } 7 8try { 9 BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); 10 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 1112for (PropertyDescriptor property : propertyDescriptors) { 13 String key = property.getName(); 1415if (!key.equals("class")) { 1617 Method getter = property.getReadMethod();// Java中提供了用来访问某个属性的 18// getter/setter方法19 Object value; 2021 value = getter.invoke(bean); 22 mp.put(key, value); 23 } 2425 } 2627 } catch (IntrospectionException e) { 2829 e.printStackTrace(); 30 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 31// TODO Auto-generated catch block32 e.printStackTrace(); 3334 } 35return mp; 3637 } 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。
原文:http://www.cnblogs.com/crows/p/4702975.html
内容总结
以上是互联网集市为您收集整理的java使用泛型实现Bean类和Map的相互转换全部内容,希望文章能够帮你解决java使用泛型实现Bean类和Map的相互转换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。