集合工具类(CollectionUtils)

package template;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.NOPTransformer;
import org.apache.commons.collections.functors.StringValueTransformer;
import org.apache.commons.collections.functors.TruePredicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collection;
import java.util.List;

/**
 * Created by wangcong on 2016/7/28.
 */
public class CollectionUtilsTemplate {
    private static final Log LOG = LogFactory.getLog(CollectionUtilsTemplate.class);

    public static void main(String[] args) {
        List<Integer> list = Lists.newArrayList();
        LOG.info(CollectionUtils.isEmpty(list));
        list.add(1);
        list.add(2);
        list.add(3);
        LOG.info(CollectionUtils.isEmpty(list));

        Collection select = CollectionUtils.select(list, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                Integer num = (Integer) object;
                return num > 2;
            }
        });
        LOG.info(select);

        CollectionUtils.transform(list, new Transformer() {
            @Override
            public Object transform(Object input) {
                Integer num = (Integer) input;
                return num+1;
            }
        });
        LOG.info(list);

        CollectionUtils.transform(list, StringValueTransformer.getInstance());
        LOG.info(list);

        Collection collection = CollectionUtils.collect(list,NOPTransformer.getInstance());
        LOG.info(collection);

        boolean isEqual = CollectionUtils.isEqualCollection(list,collection);
        LOG.info(isEqual);

        List<Integer> nums = Lists.newArrayList();
        nums.add(2);
        nums.add(5);
        CollectionUtils.transform(nums,StringValueTransformer.getInstance());
        //找出两个集合交叉的元素
        Collection intersection = CollectionUtils.intersection(list,nums);
        LOG.info(intersection);

        boolean containsAny = CollectionUtils.containsAny(nums,list);
        LOG.info(containsAny);

        //找出在list存在,在nums不存在的元素集合
        Collection subtract = CollectionUtils.subtract(list,nums);
        LOG.info(subtract);
        Collection union = CollectionUtils.union(nums,list);
        LOG.info(union);

        //nums集合中有一个符合Predicate即return true
        boolean exists = CollectionUtils.exists(nums, TruePredicate.getInstance());
        LOG.info(exists);

        //返回任何集合形式的长度、collection、map、Iterator、数组、Enum等
        int size = CollectionUtils.size(nums);
        LOG.info(size);

    }
}

  

转载于:https://www.cnblogs.com/dacong-/p/6209943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值