apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用
BeanUtils工具包是由Apache公司所開(kāi)發(fā),主要是方便程序員對(duì)Bean類(lèi)能夠進(jìn)行簡(jiǎn)便的操作。
BeanUtils一共分4個(gè)包:
????? org.apache.commons.beanutils
????? org.apache.commons.beanutils.converters
????? org.apache.commons.beanutils.locale
????? org.apache.commons.beanutils.locale.converters
其中需要我們特別關(guān)注的是這個(gè)org.apache.commons.beanutils包,其他包都是起輔助作用的。其中上面兩個(gè)沒(méi)有針對(duì)本地化的任何處理,執(zhí)行效率高。下面兩個(gè)對(duì)本地化有要求。
BeanUtils類(lèi)
主要提供了對(duì)于JavaBean進(jìn)行各種操作,比如對(duì)象,屬性復(fù)制等等。
BeanUtils設(shè)置屬性值的時(shí)候,如果屬性是基本數(shù)據(jù)類(lèi)型,那么BeanUtils會(huì)自動(dòng)幫我們進(jìn)行數(shù)據(jù)類(lèi)型的轉(zhuǎn)換,并且BeanUtils設(shè)置屬性的時(shí)候也是依賴(lài)于底層的getter和setter方法。如果設(shè)置的屬性值是其他的引用數(shù)據(jù)類(lèi)型,此時(shí)必須要注冊(cè)一個(gè)類(lèi)型轉(zhuǎn)換器才能實(shí)現(xiàn)自動(dòng)的轉(zhuǎn)換(參考下面的ConvertUtils)
常用方法:
| cloneBean(Object?bean) | 對(duì)象的克隆 |
| copyProperties(Object?dest,?Object?orig) | 對(duì)象的拷貝 |
| copyProperty(Object?bean,?String?name,?Object?value) | 拷貝指定的屬性值到指定的bean |
| setProperty(Object?bean,?String?name,?Object?value) | 設(shè)置指定屬性的值 |
| populate(Object?bean,?Map<String,? extends?Object>?properties) | 將map數(shù)據(jù)拷貝到javabean中(map的key要與javabean的屬性名稱(chēng)一致) |
注:copyProperty與setProperty,日常使用時(shí)推薦copyProperty。如果我們需要自定義實(shí)現(xiàn)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數(shù)據(jù)拷貝到j(luò)avabean中Animal a1 = new Animal();BeanUtils.populate(a1, map);System.out.println(a1); // Animal [name=tuantuan, age=3, color=black, sex=female]// 對(duì)象的拷貝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]// 設(shè)置指定的屬性BeanUtils.setProperty(a3, "sex", "male");System.out.println(a3); // Animal [name=yuanyuan, age=0, color=null, sex=male]// 克隆對(duì)象Object object = BeanUtils.cloneBean(a3);System.out.println(object); // Animal [name=yuanyuan, age=0, color=null, sex=male] }
ConvertUtils
這個(gè)工具類(lèi)的職能是在字符串和指定類(lèi)型的實(shí)例之間進(jìn)行轉(zhuǎn)換。?實(shí)際上,BeanUtils是依賴(lài)ConvertUtils來(lái)完成類(lèi)型轉(zhuǎn)換,但是有時(shí)候我們可能需要自定義轉(zhuǎn)換器來(lái)完成特殊需求的類(lèi)型轉(zhuǎn)換;
主要方法:
| convert(Object?value,?Class<?>?targetType) | 將給定的value轉(zhuǎn)換成指定的Class類(lèi)型 |
自定義類(lèi)型轉(zhuǎn)換:
自定義轉(zhuǎn)換器,實(shí)現(xiàn)Converter接口,重寫(xiě)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"); // 注冊(cè)該轉(zhuǎn)換器 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設(shè)置屬性時(shí),自動(dòng)進(jìn)行類(lèi)型轉(zhuǎn)換 MyConverter converter = new MyConverter("yyyy-MM-dd HH:mm:ss"); // 注冊(cè)該轉(zhuǎn)換器 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這兩個(gè)類(lèi)幾乎有一摸一樣的功能,唯一的區(qū)別是:BeanUtils在對(duì)Bean賦值是會(huì)進(jìn)行類(lèi)型轉(zhuǎn)化。舉例來(lái)說(shuō)也就是在copyProperty時(shí)只要屬性名相同,就算類(lèi)型不同,BeanUtils也可以進(jìn)行copy;而PropertyBean則可能會(huì)報(bào)錯(cuò)!!當(dāng)然2個(gè)Bean之間的同名屬性的類(lèi)型必須是可以轉(zhuǎn)化的,否則用BeanUtils一樣會(huì)報(bào)錯(cuò)。若實(shí)現(xiàn)了org.apache.commons.beanutils.Converter接口則可以自定義類(lèi)型之間的轉(zhuǎn)化。由于不做類(lèi)型轉(zhuǎn)化,用PropertyUtils在速度上會(huì)有很大提高!
CollectionUtils
利用這個(gè)工具類(lèi),我們對(duì)集合進(jìn)行修改、查詢(xún)、過(guò)濾等操作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// 兩個(gè)集合的比較 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] // 取交集的補(bǔ)集 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]備注:
整理的不完善,待后期完善
總結(jié)
以上是生活随笔為你收集整理的apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何将小工具添加回Windows 8和1
- 下一篇: java读取类字段名-BeanUtils