CollectionsUtil 使用

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);
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值