zoukankan      html  css  js  c++  java
  • 集合工具类(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);
    
        }
    }
    

      

  • 相关阅读:
    中断与异常
    轻松搞定C语言中复杂的声明
    C/C++中数组转换成指针的情况
    Linux下C程序的内存布局
    Java并发和多线程(二)Executor框架
    Java并发和多线程(一)基础知识
    java项目的划分方式:模块优先还是层优先?
    站在面试官角度看面试
    windows环境搭建禅道项目管理工具
    Linux环境搭建禅道项目管理工具
  • 原文地址:https://www.cnblogs.com/dacong-/p/6209943.html
Copyright © 2011-2022 走看看