码迷,mamicode.com
首页 > 其他好文 > 详细

集合工具类(CollectionUtils)

时间:2016-12-22 11:00:17      阅读:455      评论:0      收藏:0      [点我收藏+]

标签:ring   元素   contain   actor   main   factory   arraylist   sts   iterator   

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);

    }
}

  

集合工具类(CollectionUtils)

标签:ring   元素   contain   actor   main   factory   arraylist   sts   iterator   

原文地址:http://www.cnblogs.com/dacong-/p/6209943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!