• 集合工具类(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);
    
        }
    }
    

      

  • 相关阅读:
    QT 5 安装 vs2017 后,出现找不到 rc.exe 问题
    git push -f
    使用druid连接池的超时回收机制排查连接泄露问题
    git 记住密码
    postgresql c library use
    jvm内存溢出分析
    Maven中使用本地JAR包
    执行Git命令时出现各种 SSL certificate problem 的解决办法
    transformer模型计算图
    jieba分词单例模式及linux权限不够情况下tmp_dir自定义
  • 原文地址:https://www.cnblogs.com/dacong-/p/6209943.html
Copyright © 2020-2023  润新知