apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用
BeanUtils工具包是由Apache公司所開發,主要是方便程序員對Bean類能夠進行簡便的操作。
BeanUtils一共分4個包:
????? org.apache.commons.beanutils
????? org.apache.commons.beanutils.converters
????? org.apache.commons.beanutils.locale
????? org.apache.commons.beanutils.locale.converters
其中需要我們特別關注的是這個org.apache.commons.beanutils包,其他包都是起輔助作用的。其中上面兩個沒有針對本地化的任何處理,執行效率高。下面兩個對本地化有要求。
BeanUtils類
主要提供了對于JavaBean進行各種操作,比如對象,屬性復制等等。
BeanUtils設置屬性值的時候,如果屬性是基本數據類型,那么BeanUtils會自動幫我們進行數據類型的轉換,并且BeanUtils設置屬性的時候也是依賴于底層的getter和setter方法。如果設置的屬性值是其他的引用數據類型,此時必須要注冊一個類型轉換器才能實現自動的轉換(參考下面的ConvertUtils)
常用方法:
| cloneBean(Object?bean) | 對象的克隆 |
| copyProperties(Object?dest,?Object?orig) | 對象的拷貝 |
| copyProperty(Object?bean,?String?name,?Object?value) | 拷貝指定的屬性值到指定的bean |
| setProperty(Object?bean,?String?name,?Object?value) | 設置指定屬性的值 |
| populate(Object?bean,?Map<String,? extends?Object>?properties) | 將map數據拷貝到javabean中(map的key要與javabean的屬性名稱一致) |
注:copyProperty與setProperty,日常使用時推薦copyProperty。如果我們需要自定義實現populate()方法,那么我們可以override setProperty()方法.
示例:
// JavaBean public class Animal {private String name;private int age;private String color;private String sex;public Animal() {}getXxx和setXxx省略……}/*** BeanUtils*/ @Test public void test1() throws Exception {Map<String, Object> map = new HashMap<String, Object>();map.put("name", "tuantuan");map.put("age", 3);map.put("color", "black");map.put("sex", "female");// 將map數據拷貝到javabean中Animal a1 = new Animal();BeanUtils.populate(a1, map);System.out.println(a1); // Animal [name=tuantuan, age=3, color=black, sex=female]// 對象的拷貝Animal a2 = new Animal();BeanUtils.copyProperties(a2, a1);System.out.println(a2);// Animal [name=tuantuan, age=3, color=black, sex=female]// 拷貝指定的屬性Animal a3 = new Animal();BeanUtils.copyProperty(a3, "name", "yuanyuan");System.out.println(a3); // Animal [name=yuanyuan, age=0, color=null, sex=null]// 設置指定的屬性BeanUtils.setProperty(a3, "sex", "male");System.out.println(a3); // Animal [name=yuanyuan, age=0, color=null, sex=male]// 克隆對象Object object = BeanUtils.cloneBean(a3);System.out.println(object); // Animal [name=yuanyuan, age=0, color=null, sex=male] }
ConvertUtils
這個工具類的職能是在字符串和指定類型的實例之間進行轉換。?實際上,BeanUtils是依賴ConvertUtils來完成類型轉換,但是有時候我們可能需要自定義轉換器來完成特殊需求的類型轉換;
主要方法:
| convert(Object?value,?Class<?>?targetType) | 將給定的value轉換成指定的Class類型 |
自定義類型轉換:
自定義轉換器,實現Converter接口,重寫convert方法
class MyConverter implements Converter {private static SimpleDateFormat format;public MyConverter(String pattern) {format = new SimpleDateFormat(pattern);}@Overridepublic Object convert(Class type, Object value) {if (value == null) {return null;}if (value instanceof String) {String tmp = (String) value;if (tmp.trim().length() == 0) {return null;} else {try {return format.parse(tmp);} catch (ParseException e) {e.printStackTrace();}}} else {throw new ConversionException("not String");}return value;} }使用:
MyConverter converter = new MyConverter("yyyy-MM-dd HH:mm:ss"); // 注冊該轉換器 ConvertUtils.register(converter, Date.class); Object object3 = ConvertUtils.convert("2017-11-29 14:04:00", Date.class); System.out.println(object3.getClass().getTypeName()); System.out.println(object3); // BeanUtils設置屬性時,自動進行類型轉換 MyConverter converter = new MyConverter("yyyy-MM-dd HH:mm:ss"); // 注冊該轉換器 ConvertUtils.register(converter, Date.class); Animal a5 = new Animal(); BeanUtils.copyProperty(a5, "birth", "2017-11-29 14:04:00"); System.out.println(a5);// Animal [name=null, age=0, color=null, sex=null, birth=Wed Nov 29 14:04:00 CST 2017]PropertyUtils
BeanUtils與PropertyUtils這兩個類幾乎有一摸一樣的功能,唯一的區別是:BeanUtils在對Bean賦值是會進行類型轉化。舉例來說也就是在copyProperty時只要屬性名相同,就算類型不同,BeanUtils也可以進行copy;而PropertyBean則可能會報錯!!當然2個Bean之間的同名屬性的類型必須是可以轉化的,否則用BeanUtils一樣會報錯。若實現了org.apache.commons.beanutils.Converter接口則可以自定義類型之間的轉化。由于不做類型轉化,用PropertyUtils在速度上會有很大提高!
CollectionUtils
利用這個工具類,我們對集合進行修改、查詢、過濾等操作CollectionUtils屬于commons-collections包
String[] arrA = new String[] { "1", "2", "3" }; String[] arrB = new String[] { "1", "a", "b" }; List<String> listA = Arrays.asList(arrA); List<String> listB = Arrays.asList(arrB);// 判斷集合是否為 空 System.out.println(CollectionUtils.isEmpty(listA));// false System.out.println(CollectionUtils.isEmpty(listB));// false// 判斷集合是否為 不為空 System.out.println(CollectionUtils.isNotEmpty(listA));// true System.out.println(CollectionUtils.isNotEmpty(listB));// true// 兩個集合的比較 System.out.println(CollectionUtils.isEqualCollection(listA, listB));// false// 集合的操作 // 取并集 System.out.println(CollectionUtils.union(listA, listB));// [1, a, 2, b, 3] // 取交集 System.out.println(CollectionUtils.intersection(listA, listB));// [1] // 取交集的補集 System.out.println(CollectionUtils.disjunction(listA, listB));// [a, 2, b, 3] // 取集合相減 System.out.println(CollectionUtils.subtract(listA, listB));// [2, 3] System.out.println(CollectionUtils.subtract(listB, listA));// [a, b]List<String> listC = new ArrayList<>(); listC.add("1"); listC.add("2"); listC.add("3"); String[] arrC = { "4", "5", "6" }; // 向集合中添加值 CollectionUtils.addAll(listC, arrC); System.out.println(listC);// [1, 2, 3, 4, 5, 6]備注:
整理的不完善,待后期完善
總結
以上是生活随笔為你收集整理的apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何将小工具添加回Windows 8和1
- 下一篇: java读取类字段名-BeanUtils