概述
package com.kd.zhouyuan.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.beanutils.BeanPropertyValueChangeClosure;
import org.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import com.kd.zhouyuan.entity.User;
/**
* CollectionUtils 测试
*
* @author 269052
*
*/
public class MyCollectionUtils {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Collection c1 = new ArrayList();
c1.add("one");
c1.add("two");
c1.add("three");
Collection c2 = new ArrayList();
c2.add("one");
c2.add("two");
Collection coll = new ArrayList();
User user1 = new User();
user1.setUserName("hello1");
user1.setPassword("12323");
User user2 = new User();
user2.setUserName("hello2");
user2.setPassword("123232");
User user3 = new User();
user3.setUserName("hello2");
user3.setPassword("122223232");
coll.add(user2);
coll.add(user1);
coll.add(user3);
/**
* 1、求两个集合的并集
*/
/*Collection c3 = CollectionUtils.union(c1, c2);
System.out.println(c3);*/
/**
* 2、求两个集合的交集
*/
//Collection c3 = CollectionUtils.intersection(c1, c2);
/**
* 3、求两个集合的差集
*/
//Collection c3 = CollectionUtils.disjunction(c1,c2);
/**
*
*/
//Collection c3 = CollectionUtils.subtract(c2,c1);
//boolean flag = CollectionUtils.containsAny(c1,c2);
/**
* 从集合中获取某个匹配的对象
*/
User c3 = (User) CollectionUtils.find(coll, new Predicate() {
@Override
public boolean evaluate(Object object) {
User user = (User)object;
if("hello2".equals(user.getUserName()))
return true;
return false;
}
});
Predicate predicate = new BeanPropertyValueEqualsPredicate("userName", "hello2");
/**
* 从集合中选出指定的集合数据
* 选出属性匹配某个值的集合
*/
Collection c5 = CollectionUtils.select(coll, predicate);
System.out.println(c5);
/**
* 将原始集合转换为另外一个集合
*
*/
/*List<String> names = (List<String>) CollectionUtils.collect(coll, new Transformer() {
@Override
public java.lang.Object transform(java.lang.Object input) {
return ((User)input).getUserName();
}
});
System.out.println(names);*/
/*@SuppressWarnings("unchecked")
List<String> codIds = (List<String>) CollectionUtils.collect(codDtoList,new Transformer(){
public java.lang.Object transform(final java.lang.Object input){
return ((CODDto)input).getId();
}
});
CODEntity entity = (CODEntity) CollectionUtils.find(codList, new Predicate() {
@Override
public boolean evaluate(Object object) {
if (StringUtils.equals(((CODEntity)object).getId(), dto.getId()))
return true;
return false;
}
});
*/
User user6 = (User) CollectionUtils.get(coll, 1);
System.out.println("22"+user6);
}
}
最后
以上就是纯真季节为你收集整理的CollectionsUtil 使用的全部内容,希望文章能够帮你解决CollectionsUtil 使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复